destructor

Discussion in 'C++' started by Ramkey, Jun 23, 2008.

  1. Ramkey

    Ramkey Guest

    I was asked in a test
    class A{
    A ~A{
    cout << "A's destructor";
    }
    };

    int main(void){
    A Aobj;
    Aobj.~A;
    }

    What will be the output?

    I wrote "A's destructor";
    I think it may also be "A's destructor" "A's destructor:(twice).I
    didnt run the program.What will be the output and why?

    Thanks
    Ramkey, Jun 23, 2008
    #1
    1. Advertising

  2. Ramkey

    Rolf Magnus Guest

    Ramkey wrote:

    > I was asked in a test
    > class A{
    > A ~A{
    > cout << "A's destructor";
    > }
    > };
    >
    > int main(void){
    > A Aobj;
    > Aobj.~A;
    > }
    >
    > What will be the output?
    >
    > I wrote "A's destructor";
    > I think it may also be "A's destructor" "A's destructor:(twice).I
    > didnt run the program.What will be the output and why?


    Ignoring the syntax errors in your code, the output is undefined. Attempting
    to destroy an object twice has undefined behavior.
    Rolf Magnus, Jun 23, 2008
    #2
    1. Advertising

  3. Ramkey

    Rolf Magnus Guest

    Dyziek wrote:

    > Rolf Magnus pisze:
    >> Ramkey wrote:
    >>
    >> Ignoring the syntax errors in your code, the output is undefined.
    >> Attempting to destroy an object twice has undefined behavior.
    >>

    >
    > it is not destroying object twice, it is running its destructor twice.


    That's what "destroying the object" means.

    > object is destroyed only once.


    No. Its memory is deallocated once.
    Rolf Magnus, Jun 23, 2008
    #3
    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. Jacob

    destructor needed

    Jacob, Oct 30, 2003, in forum: Java
    Replies:
    5
    Views:
    860
    David Holliday
    Oct 31, 2003
  2. frs
    Replies:
    20
    Views:
    725
    Alf P. Steinbach
    Sep 21, 2005
  3. arun
    Replies:
    2
    Views:
    521
    benben
    Jun 13, 2006
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    403
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,155
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page