By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

problem in calling constructors and destructors

P: n/a
hello,
I write a following program and have problem in understanding
constructors and destructors.
#include <iostream.h>
class vector
{
public:

double x;
double y;
static int cnt,dst;
vector (double = 0, double = 0);
~vector ();
vector operator + (vector);
};

int vector::cnt=1;
int vector::dst=1;
vector::~vector ()
{
cout<<"called destructor "<<dst++<<"times\n";
}

vector::vector (double a, double b)
{
cout<<"called constructor "<<cnt++<<"times\n";
x = a;
y = b;
}

vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}

ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")\n";
return o;
}

int main ()
{
vector a;
vector b;
vector c (3, 5);
a = b + c;
cout << "The content of vector a: " << a;
return 0;
}

/*
called constructor 1times
called constructor 2times
called constructor 3times
called constructor 4times
called destructor 1times
called destructor 2times
The content of vector a: (3, 5)
called destructor 3times
called destructor 4times
called destructor 5times
called destructor 6times
*/
From above program what i get is that class objects a,b,c calls

constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.
I am referring my output for numbering constructors and destructors.

Aug 5 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

ra*******@gmail.com wrote:

[]
From above program what i get is that class objects a,b,c calls

constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.


To see the whole picture implement and add logging in
vector::vector(vector const&) and vector& operator=(vector const&);

Aug 5 '05 #2

P: n/a
ra*******@gmail.com wrote:
[]
From above program what i get is that class objects a,b,c calls

constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.
I am referring my output for numbering constructors and destructors.


Do You have a default constructor with 'cout<<"called constructor
"<<cnt++<<"times\n";' instruction? I don't see it in your code, but you
assume that it should print text due to construct objects a and b. I'm
sure you have not copy constructor with print instruction.
First of all your operator+ and operator<< work with *values* and not
*references*. It should looks like this:

vector& vector::operator + (vector& a)
{
return vector (x + a.x, y + a.y);
}

ostream& operator << (ostream& o, vector& a)
{
o << "(" << a.x << ", " << a.y << ")\n";
return o;
}

You have two destructor before cout because two automatic object are
destroyed (one from created new object for parameter operator+ and one
for returning new object for this operator).

Third destructor you have question about (after cout) is for automatic
object created for this cout (operator<<).

Hope this cleared something
regards Michal
Aug 5 '05 #3

P: n/a

"misiek3d" <mi***********@poczta.onet.pl> wrote in message
news:dc**********@nemesis.news.tpi.pl...
ra*******@gmail.com wrote:
[]
From above program what i get is that class objects a,b,c calls constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.
I am referring my output for numbering constructors and destructors.


Do You have a default constructor with 'cout<<"called constructor
"<<cnt++<<"times\n";' instruction? I don't see it in your code, but you
assume that it should print text due to construct objects a and b. I'm
sure you have not copy constructor with print instruction.
First of all your operator+ and operator<< work with *values* and not
*references*.


Yes and no, this depends!
It should looks like this:

vector& vector::operator + (vector& a)
{
return vector (x + a.x, y + a.y);
}

No, it shouldn't. You're returning a reference to locally created object,
which is a bad thing! I'd recommend to read the FAQ section 13.9
ostream& operator << (ostream& o, vector& a)
{
o << "(" << a.x << ", " << a.y << ")\n";
return o;
}


In this case it's okay because the already existing stream was passed..

[SNIP]

Cheers
Chris
Aug 5 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.