problem in calling constructors and destructors

R

rahul8143

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::eek:perator + (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.
 
M

Maxim Yegorushkin

(e-mail address removed) wrote:

[]
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&);
 
M

misiek3d

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::eek:perator + (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
 
C

Chris Theis

misiek3d said:
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::eek:perator + (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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top