426,115 Members | 894 Online Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

# Why is the Constructor called 4 times but the Destructor 5 times?

 P: n/a Why is the Constructor called 4 times but the Destructor 5 times? I am using MS VC 6. If this has been covered already please let me know. The Code: #include class MyClass { private: int x; int y; static int count; int id; public: MyClass(int x=0,int y=0): x(x),y(y){ printf("Create (%d)\n",++count); id = count; } ~MyClass(){ printf("kill (%d)\n",id); } MyClass(const MyClass &r){ printf("Copy\n"); x = r.x; y = r.y; } void Set(int ix,int iy){ x = ix; y = iy; } void Print(){ printf("x=%d y=%d\n",x,y); } //operators MyClass operator + (const MyClass &left){ printf("Start +\n"); MyClass temp; temp.x = x + left.x; temp.y = y + left.y; printf("End +\n"); return temp; } MyClass &operator += (const MyClass &left){ x += left.x; y += left.y; return *this; } MyClass &operator += (int left){ x += left; y += left; return *this; } MyClass &operator = (const MyClass &r){ x = r.x; y = r.y; return *this; } }; int MyClass::count = 0; int main(int argc,char **argv){ MyClass c1(5,6); MyClass c2(1,1); MyClass c3(100,200); printf("before\n"); c1.Print(); c2.Print(); c3.Print(); c3 = c1+ c2; printf("after\n"); c1.Print(); c2.Print(); c3.Print(); return 0; } Jul 19 '05 #1
9 Replies

 P: n/a "djskrill" wrote in message news:f0**************************@posting.google.c om... Why is the Constructor called 4 times but the Destructor 5 times? I am using MS VC 6. If this has been covered already please let me know. A constructor is called 5 times. After modifying your copy constructor to actually initialize the id field: MyClass& operator=(const MyClass& r) { id = r. id; x = r.x; y = r.y; } This is what I get as output: (annoated) Create (1) // main c1 variable Create (2) // main c2 variable Create (3) // main c3 variable before x=5 y=6 x=1 y=1 x=100 y=200 Start + Create (4) // operator+ temp End + Copy (5) // return value temporary kill (4) kill (5) after x=5 y=6 x=1 y=1 x=6 y=7 kill (3) kill (2) kill (1) Which Jul 19 '05 #2

 P: n/a "djskrill" wrote in message news:f0**************************@posting.google.c om... Why is the Constructor called 4 times but the Destructor 5 times? I am using MS VC 6. If this has been covered already please let me know. The Code: Please indent your code. It's not very readable without it. #include Why aren't you using iostreams? class MyClass { private: int x; int y; static int count; int id; public: MyClass(int x=0,int y=0): x(x),y(y){ printf("Create (%d)\n",++count); id = count; } ~MyClass(){ printf("kill (%d)\n",id); } MyClass(const MyClass &r){ This is a constructor. A 'copy constructor'. printf("Copy\n"); x = r.x; y = r.y; You've left 'count' uninitialized. Evalutating its value will give undefined behavior. } Also, why aren't you using initializer list? MyClass(const MyClass &r) : x(r.x), y(r.y), count(r.count) { printf("Copy\n"); } More below. void Set(int ix,int iy){ x = ix; y = iy; } void Print(){ printf("x=%d y=%d\n",x,y); } //operators MyClass operator + (const MyClass &left){ printf("Start +\n"); MyClass temp; temp.x = x + left.x; temp.y = y + left.y; printf("End +\n"); return temp; } MyClass &operator += (const MyClass &left){ x += left.x; y += left.y; return *this; } MyClass &operator += (int left){ x += left; y += left; return *this; } MyClass &operator = (const MyClass &r){ x = r.x; y = r.y; return *this; } }; int MyClass::count = 0; int main(int argc,char **argv){ MyClass c1(5,6); MyClass c2(1,1); MyClass c3(100,200); printf("before\n"); c1.Print(); c2.Print(); c3.Print(); c3 = c1+ c2; printf("after\n"); c1.Print(); c2.Print(); c3.Print(); return 0; } I get output with VC++6.0 SP5: Create (1) Create (2) Create (3) before x=5 y=6 x=1 y=1 x=100 y=200 Start + Create (4) End + Copy <<== note that this is a ctor call kill (4) kill (-858993460) <<== this alerted me to the 'uninitialized' problem after x=5 y=6 x=1 y=1 x=6 y=7 kill (3) kill (2) kill (1) I see five ctor calls, and five dtor calls. -Mike Jul 19 '05 #3

 P: n/a "Mike Wahler" wrote in message news:%V*****************@newsread4.news.pas.earthl ink.net... MyClass(const MyClass &r){ This is a constructor. A 'copy constructor'. After reading Ron's reply, I realized I have erred. printf("Copy\n"); x = r.x; y = r.y; You've left 'count' uninitialized. 'id' Evalutating its value will give undefined behavior. } Also, why aren't you using initializer list? MyClass(const MyClass &r) : x(r.x), y(r.y), count(r.count) MyClass(const MyClass &r) : x(r.x), y(r.y), id(r.id) Sorry about that. That's what I get for not compiling and testing my corrections. :-) -Mike Jul 19 '05 #4

 P: n/a "Mike Wahler" wrote in message news:%V*****************@newsread4.news.pas.earthl ink.net... Please indent your code. It's not very readable without it. His code is indented. It's just that your news reader (presumably outlook express) is eating the tabs on display. You've left 'count' uninitialized. Evalutating its value will give undefined behavior. Actually, he left id uninitialized. Count is a static. Jul 19 '05 #5

 P: n/a "Mike Wahler" wrote in message news:2Z*****************@newsread4.news.pas.earthl ink.net... MyClass(const MyClass &r) : x(r.x), y(r.y), id(r.id) Sorry about that. That's what I get for not compiling and testing my corrections. :-) Actually, he doesn't want to copy the id. He wants to give it a new serial number, otherwise it's impossible to pair up the destructors. Jul 19 '05 #6

 P: n/a "Ron Natalie" wrote in message news:3f***********************@news.newshosting.co m... "Mike Wahler" wrote in message news:2Z*****************@newsread4.news.pas.earthl ink.net... MyClass(const MyClass &r) : x(r.x), y(r.y), id(r.id) Sorry about that. That's what I get for not compiling and testing my corrections. :-) Actually, he doesn't want to copy the id. He wants to give it a new serial number, otherwise it's impossible to pair up the destructors. Oh, yes, overlooked that. -Mike Jul 19 '05 #7

 P: n/a "Mike Wahler" wrote in message news:<%V*****************@newsread4.news.pas.earth link.net>... [snip] MyClass(const MyClass &r){ This is a constructor. A 'copy constructor'. printf("Copy\n"); x = r.x; y = r.y; You've left 'count' uninitialized. Evalutating its value will give undefined behavior. } Also, why aren't you using initializer list? MyClass(const MyClass &r) : x(r.x), y(r.y), count(r.count) { printf("Copy\n"); } but count is static and need not be initialized in constructors. I believe you meant id. But then again, the proper way would be: MyClass(const MyClass &r) : x(r.x), y(r.y) { ++count; id = count; printf("Copy create (%d)\n",++count); } which would correct two problems: the garbage you get when printing id at destruction time, and accounting for the construction of the object. Regards, Marcelo Pinto Jul 19 '05 #8

 P: n/a mp****@tecnolink.com.br (Marcelo Pinto) wrote in message news:... "Mike Wahler" wrote in message news:<%V*****************@newsread4.news.pas.earth link.net>... [snip] but count is static and need not be initialized in constructors. I believe you meant id. But then again, the proper way would be: MyClass(const MyClass &r) : x(r.x), y(r.y) { ++count; id = count; printf("Copy create (%d)\n",++count); [snip] forgive me the ++count inside the printf, it should be: printf("Copy create (%d)\n",count); Regards, Marcelo Pinto Jul 19 '05 #9

 P: n/a Thanks for the help. Jul 19 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion. 