Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
using namespace std;
#include<conio.h>
class cpte_obj{
      static int ctr;
      public:
             cpte_obj();
             ~cpte_obj();
             static void compte();
             };
             int cpte_obj::ctr;
             cpte_obj::cpte_obj(){
                                  cout<<"++construction : il y a maintenant "<<++ctr<<" objets"<<endl;
                                  }
             cpte_obj::~cpte_obj(){
                                   cout<<"--destruction : il reste maintenant "<<--ctr<<" objets"<<endl;
                                   }
             void cpte_obj::compte(){
                  cout<<"appel compte : il y a "<<ctr<<" objets"<<endl;
                  }
                  main(){
                         void fct();
                         cpte_obj::compte();
                         cpte_obj a;
                         cpte_obj::compte();
                         fct();
                         cpte_obj::compte();
                         cpte_obj b;           
                         cpte_obj::compte();
                         getche();
                         }
                         void fct(){
                              cpte_obj u,v;
                              }
                              
Ce programme donne l'affichage suivant :

appel compte : il y a 0 objets
++construction : il y a maintenant 1 objets
appel compte : il y a 1 objets
++construction : il y a maintenant 2 objets
++construction : il y a maintenant 3 objets
--destruction : il reste maintenant 2 objets
--destruction : il reste maintenant 1 objets

appel compte : il y a 1 objets
++construction : il y a maintenant 2 objets
appel compte : il y a 2 objets


Mon problème est que je n'ai pas compris comment lorsque j'appelle la fonction : void fct() , celle-ci passe à l'utilisation de la fonction cpte_obj::~cpte_obj() et par suite à l'affichage de ces 2 lignes :
--destruction : il reste maintenant 2 objets
--destruction : il reste maintenant 1 objets