473,385 Members | 2,028 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,385 software developers and data experts.

3 destruction calls

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?
Jul 19 '05 #1
5 1841
Gandalf wrote:
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

Jul 19 '05 #2
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.

Jul 19 '05 #3
Hang on...
isn't the default CC compiler dependent? that would explain it! (I guess)
Jul 19 '05 #4
Michael Demanet wrote:

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

Jul 19 '05 #5

"Gandalf" <ga*****@gunix.dk> wrote in message
news:_s*******************@newsb.telia.net...
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.
Jul 19 '05 #6

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

Similar topics

0
by: Stephan Keil | last post by:
Hi all, consider a class class X { static X& OneX() { static X sgl1; return sgl1; } static X& TheX() { static X sgl2(OneX()); return sgl2; } }; and two source files with global variables
3
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I...
5
by: Pierre-Eric.Melchy | last post by:
Hello, I have a class measurement representing a physical measurement. Different objects in this class represent laboratory equipment, which might raise an exception (e.g. overtemperature). ...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
3
by: Neal Becker | last post by:
Code at global scope in a module is run at module construction (init). Is it possible to hook into module destruction (unloading)?
8
by: REH | last post by:
This curiosity popped into my head on the way to work today. I wouldn't actually do this, but just wondering. Is the following defined behavior? #include <new> class T { }; int main() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.