Connecting Tech Pros Worldwide Help | Site Map

when is destructor invoked?

  #1  
Old July 22nd, 2005, 12:25 PM
Charles Jamieson
Guest
 
Posts: n/a
I declare a class

class myClass{
public:
~myClass();
  #2  
Old July 22nd, 2005, 12:25 PM
Jacques Labuschagne
Guest
 
Posts: n/a

re: when is destructor invoked?


Charles Jamieson wrote:[color=blue]
> obj.~myClass();
>
> Had I placed this line in main(), when main() is exited the
> destructor would not be called again.[/color]

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.
  #3  
Old July 22nd, 2005, 12:25 PM
David Harmon
Guest
 
Posts: n/a

re: when is destructor invoked?


On Sat, 22 May 2004 02:22:34 GMT in comp.lang.c++, Charles Jamieson
<cjamieson@no.junk> wrote,[color=blue]
>Inside this function I destroy the object as follows
>
> obj.~myClass();[/color]

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/

  #4  
Old July 22nd, 2005, 12:26 PM
Rolf Magnus
Guest
 
Posts: n/a

re: when is destructor invoked?


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

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.
[color=blue]
> 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.[/color]

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

  #5  
Old July 22nd, 2005, 12:26 PM
JKop
Guest
 
Posts: n/a

re: when is destructor invoked?


Charles Jamieson posted:
[color=blue]
> 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
>[/color]


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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
destructor invoked without constructor. sam_cit@yahoo.co.in answers 13 May 4th, 2007 02:35 PM
Call constructor/destructor of base class Fabian Müller answers 23 July 22nd, 2005 05:07 PM