473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

3 destruction calls

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

class Foo{
public:
Foo(){cout<<"Ma king"<<endl;}
~Foo(){cout<<"D estroying"<<end l;}
};

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 1851
Gandalf wrote:
Hello (it's the newbie again).
If I have a class

class Foo{
public:
Foo(){cout<<"Ma king"<<endl;}
~Foo(){cout<<"D estroying"<<end l;}
};

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::end l;}*/
~Foo(){std::cou t<<"Destroying" <<std::endl;}
/*Foo& operator=(const Foo&){std::cout <<"Copying"<<st d::endl;}*/
};

void func(Foo x)
{
std::cout<<"ins ide func"<<std::end l;
}

int main()
{
Foo a;
std::cout<<"bef ore calling func"<<std::end l;
func(a);
std::cout<<"aft er calling func"<<std::end l;

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::end l;}
~Foo(){std::cou t<<"Destroying" <<std::endl;}
Foo& operator=(const Foo&){std::cout <<"Copying"<<st d::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******** ***********@new sb.telia.net...
Hello (it's the newbie again).
If I have a class

class Foo{
public:
Foo(){cout<<"Ma king"<<endl;}
~Foo(){cout<<"D estroying"<<end l;}
};

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
1227
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
5193
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 have a std::vector<SPoly> xtop; where SPoly simply is: struct SPoly { int start, end;
5
1335
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). In any case the equipment has to be switched off after the experiment, since if a power supply stays in the on state for a prolonged time equipment...
9
2993
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. For those that have no idea what I am talking about (I learn a lot reading posts on subjects in which I am clueless), consider the following code...
6
2241
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 contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the...
2
4305
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
3396
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 class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just...
3
2368
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
1775
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
7615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.