Connecting Tech Pros Worldwide Forums | Help | Site Map

3 destruction calls

Gandalf
Guest
 
Posts: n/a
#1: Jul 19 '05
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?

Michael Demanet
Guest
 
Posts: n/a
#2: Jul 19 '05

re: 3 destruction calls


Gandalf wrote:[color=blue]
> 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);
> }[/color]

#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

Gandalf
Guest
 
Posts: n/a
#3: Jul 19 '05

re: 3 destruction calls


Thanks for your reply Michael,
[color=blue]
> c++ -Wall copie.cpp
> $ ./a.out
> Making
> before calling func
> inside func
> Destroying
> after calling func
> Destroying[/color]
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![color=blue]
> 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...[/color]
This temporary object? What about x? isn't your first call to destructor
the destruction of x?
[color=blue]
> Also 2 calls of destroys. Are you sure ?[/color]
Yes I also get 2 destructions when I have a CC.

Gandalf
Guest
 
Posts: n/a
#4: Jul 19 '05

re: 3 destruction calls


Hang on...
isn't the default CC compiler dependent? that would explain it! (I guess)
Kevin Goodsell
Guest
 
Posts: n/a
#5: Jul 19 '05

re: 3 destruction calls


Michael Demanet wrote:
[color=blue]
>
> 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...[/color]

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

jeffc
Guest
 
Posts: n/a
#6: Jul 19 '05

re: 3 destruction calls



"Gandalf" <gandalf@gunix.dk> wrote in message
news:_s3ab.24836$mU6.55665@newsb.telia.net...[color=blue]
> 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.[/color]

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.


Closed Thread