3 destruction calls

G

Gandalf

Hello (it's the newbie again).
If I have a class

class Foo{
public:
Foo(){cout<<"Making"<<endl;}
~Foo(){cout<<"Destroying"<<endl;}
};

void func(Foo x){}
int main(){
Foo a;
func(a);
}

I see three calls to the destructor. I assume these are,
the third = the destruction of object a in main
the second= the destruction of the object x in function "func"
the first = creation of a temporary copy of a, that is copied into x and
then destroyed?

I'm not sure about the first call to the destructor, is there a temp.
object created?
The first call to ~Foo is not there when I have a CC defined, and that's
because object x is created by the cc which in this case looks like
Foo(const Foo& cc){cout<<"CC"<<endl;}
But how is this any different from the default CC that is called in the
first case when I don't have any CC? (since I gues that the default CC
looks not too different from my CC, i.e. does nothing)

Can someone straighten this out for me?
 
M

Michael Demanet

Gandalf said:
Hello (it's the newbie again).
If I have a class

class Foo{
public:
Foo(){cout<<"Making"<<endl;}
~Foo(){cout<<"Destroying"<<endl;}
};

void func(Foo x){}
int main(){
Foo a;
func(a);
}

#include <iostream>

class Foo
{
public:
Foo(){std::cout<<"Making"<<std::endl;}
/*Foo(const Foo&){std::cout<<"Making copy"<<std::endl;}*/
~Foo(){std::cout<<"Destroying"<<std::endl;}
/*Foo& operator=(const Foo&){std::cout<<"Copying"<<std::endl;}*/
};

void func(Foo x)
{
std::cout<<"inside func"<<std::endl;
}

int main()
{
Foo a;
std::cout<<"before calling func"<<std::endl;
func(a);
std::cout<<"after calling func"<<std::endl;

return EXIT_SUCCESS;
}

c++ -Wall copie.cpp
$ ./a.out
Making
before calling func
inside func
Destroying
after calling func
Destroying

I only see 2 destroying process : one for the temporary object created
to pass a --> x and one for the a in main when a is out of scope...

Now with Copy constructor :

class Foo
{
public:
Foo(){std::cout<<"Making"<<std::endl;}
Foo(const Foo&){std::cout<<"Making copy"<<std::endl;}
~Foo(){std::cout<<"Destroying"<<std::endl;}
Foo& operator=(const Foo&){std::cout<<"Copying"<<std::endl; return *this;}
};

c++ -Wall copie.cpp
$ ./a.out
Making
before calling func
Making copy
inside func
Destroying
after calling func
Destroying

Also 2 calls of destroys. Are you sure ?

Michaël
 
G

Gandalf

Thanks for your reply Michael,
c++ -Wall copie.cpp
$ ./a.out
Making
before calling func
inside func
Destroying
after calling func
Destroying
On my Linux box (with that crappy old g++ compiler 2.95.4) I get with your
code

Making
before calling func
inside func
Destroying
Destroying
after calling func
Destroying

Strange!
I only see 2 destroying process : one for the temporary object created
to pass a --> x and one for the a in main when a is out of scope...
This temporary object? What about x? isn't your first call to destructor
the destruction of x?
Also 2 calls of destroys. Are you sure ?
Yes I also get 2 destructions when I have a CC.
 
G

Gandalf

Hang on...
isn't the default CC compiler dependent? that would explain it! (I guess)
 
K

Kevin Goodsell

Michael said:
I only see 2 destroying process : one for the temporary object created
to pass a --> x and one for the a in main when a is out of scope...

There is no "temporary object" created. 'x' is copy-constructed from 'a'
with no temporary in between. If there *were* a temporary, then the 3
destructor calls would make sense: 1 for 'a', 1 for 'x', and 1 for the
temporary.

As it is, I don't know why the OP would see 3 destructor calls.

-Kevin
 
J

jeffc

Gandalf said:
Hello (it's the newbie again).
If I have a class

class Foo{
public:
Foo(){cout<<"Making"<<endl;}
~Foo(){cout<<"Destroying"<<endl;}
};

void func(Foo x){}
int main(){
Foo a;
func(a);
}

I see three calls to the destructor.

Doesn't sound right to me. I was working with some code exception code
using Visual C++ and Microsoft's CString class. The exception wasn't
working right because there was some sort of bug that caused the destructor
for a CString to get called twice. I switched to the standard library
string, and it worked correctly with one destructor call. Could be a bug
you have there.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top