Hello hello,
Je m'initie au C++ avec le livre Programmez en langage C++ de Claude Delannoy (edition eyrolles) et je l'aime bien (pour le moment) mais arrive un exemple ou je comprend pas tout à fait qqchose (bon j'en suis pas très loin) :
Donc ma question concernant les lignes rouges *1 et *2, pourquoi *1 créé 2 objets et *2 en détruit 2 ???
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
36
37
38
39
40
41
42
43 #include <iostream> using namespace std; class cpte_obj { static int ctr; public : cpte_obj(); ~cpte_obj(); }; int cpte_obj::ctr = 0; cpte_obj::cpte_obj() { cout << "++ construction : il y a maintenant " << ++ctr << " objets\n"; } cpte_obj::~cpte_obj() { cout << "-- destruction : il y a maintenant " << --ctr << " objets\n"; } main() { void fct(); // *1 cpte_obj a; fct(); // *2 cpte_obj b; } void fct() { cpte_obj u,v; } } et cela retourne : ++ construction : il y a maintenant 1 objets ++ construction : il y a maintenant 2 objets ++ construction : il y a maintenant 3 objets -- destruction : il y a maintenant 2 objets -- destruction : il y a maintenant 1 objets ++ construction : il y a maintenant 2 objets -- destruction : il y a maintenant 1 objets -- destruction : il y a maintenant 0 objets
Merci d'avance pour votre aide !!!
Partager