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

D

djskrill

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 <stdio.h>

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;
}
 
R

Ron Natalie

djskrill said:
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
 
M

Mike Wahler

djskrill said:
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 <stdio.h>

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
 
M

Mike Wahler

This is a constructor. A 'copy constructor'.

After reading Ron's reply, I realized I have erred.
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
 
R

Ron Natalie

Mike Wahler said:
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.
 
R

Ron Natalie

Mike Wahler said:
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.
 
M

Mike Wahler

Ron Natalie said:
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
 
M

Marcelo Pinto

Mike Wahler said:
This is a constructor. A 'copy constructor'.


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
 
M

Marcelo Pinto

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top