Connecting Tech Pros Worldwide Help | Site Map

when is destructor invoked?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:25 AM
Charles Jamieson
Guest
 
Posts: n/a
Default when is destructor invoked?

I declare a class

class myClass{
public:
~myClass();

  #2  
Old July 22nd, 2005, 11:25 AM
Jacques Labuschagne
Guest
 
Posts: n/a
Default 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, 11:25 AM
David Harmon
Guest
 
Posts: n/a
Default 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, 11:26 AM
Rolf Magnus
Guest
 
Posts: n/a
Default 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, 11:26 AM
JKop
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.