when is destructor invoked?

Discussion in 'C++' started by Charles Jamieson, May 22, 2004.

  1. I declare a class

    class myClass{
    public:
    ~myClass();
     
    Charles Jamieson, May 22, 2004
    #1
    1. Advertising

  2. Charles Jamieson wrote:
    > 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.
     
    Jacques Labuschagne, May 22, 2004
    #2
    1. Advertising

  3. Charles Jamieson

    David Harmon Guest

    On Sat, 22 May 2004 02:22:34 GMT in comp.lang.c++, Charles Jamieson
    <> wrote,
    >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/
     
    David Harmon, May 22, 2004
    #3
  4. Charles Jamieson

    Rolf Magnus Guest

    Charles Jamieson wrote:

    > 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.
     
    Rolf Magnus, May 22, 2004
    #4
  5. Charles Jamieson

    JKop Guest

    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
     
    JKop, May 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frs
    Replies:
    20
    Views:
    777
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    555
    benben
    Jun 13, 2006
  3. Replies:
    13
    Views:
    548
    James Kanze
    May 4, 2007
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    427
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,175
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page