when is destructor invoked?

J

Jacques Labuschagne

Charles said:
obj.~myClass();

Had I placed this line in main(), when main() is exited the
destructor would not be called again.

Says who?
Section 12.4/14 of the standard says "Once a destructor is invoked for
an object, the object no longer exists; the behaviour is undefined if
the destructor is invoked for an object whose lifetime has ended.
[Example: if the destructor for an automatic object is explicitly
invoked, and the block is subsequently left in a manner that would
ordinarily invoke implicit destruction of the object, behaviour is
undefined.]"

Jacques.
 
D

David Harmon

On Sat, 22 May 2004 02:22:34 GMT in comp.lang.c++, Charles Jamieson
Inside this function I destroy the object as follows

obj.~myClass();

Never do that.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[11.5] Should I explicitly call a destructor on a local variable?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
R

Rolf Magnus

Charles said:
I declare a class

class myClass{
public:
~myClass();
.
.
.
}

In main() I have the line

myClass obj;


I then invoke a function with the prototype

void func( myClass& obj );

Inside this function I destroy the object as follows

obj.~myClass();

Had I placed this line in main(), when main() is exited the
destructor would not be called again.

Huh? Why? Local objects in main get automatically destroyed when they go
out of scope, just like in any other function. You're not allowed to
call the destructor yourself here.
Since I passed the object by reference, I expect that anything I do in
the function is the same had I done it in the calling routine. Yet in
this case, when main terminates, the destructor is invoked again.

That should be independant of any function call within main and should
be what always happens.
 
J

JKop

Charles Jamieson posted:
I declare a class

class myClass{
public:
~myClass();
.
.
.
}

In main() I have the line

myClass obj;


I then invoke a function with the prototype

void func( myClass& obj );

Inside this function I destroy the object as follows

obj.~myClass();

Had I placed this line in main(), when main() is exited the
destructor would not be called again.

Since I passed the object by reference, I expect that anything I do in
the function is the same had I done it in the calling routine. Yet in
this case, when main terminates, the destructor is invoked again.

What is the reasoning behind this behavior?

-charles


Well it seems to me that you want to pick right where and when the
contructor and destructor are invoked. Try braces:

int main(void)
{
...

{
myClass obj; //Constructor invoked HERE
...

} //Destructor invoked HERE

...
}


Or maybe I misinterpreted your intentions!

-JKop
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top