473,411 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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;
}
Jul 19 '05 #1
9 1696

"djskrill" <cp*@twerd.com> 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

"djskrill" <cp*@twerd.com> 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 <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
Jul 19 '05 #3

"Mike Wahler" <mk******@mkwahler.net> 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

"Mike Wahler" <mk******@mkwahler.net> 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

"Mike Wahler" <mk******@mkwahler.net> 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

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Mike Wahler" <mk******@mkwahler.net> 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
"Mike Wahler" <mk******@mkwahler.net> 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
mp****@tecnolink.com.br (Marcelo Pinto) wrote in message news:<e3**************************@posting.google. com>...
"Mike Wahler" <mk******@mkwahler.net> 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
Thanks for the help.
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
6
by: giles | last post by:
Hi, Does a compiler generated constructor initialise the data members? Giles
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: JAL | last post by:
According to MSDN2, if a managed class throws an exception in the constructor, the destructor will be called. If an exception is thrown in the constructor the object never existed so how in the...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
8
by: Kannan | last post by:
Some amount of memory is allocated inside the Base Constructor using new. During the construction of a derived object an exception occurred in the constructor of the derived class. Will the...
20
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.