CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2007
    Posts
    4

    Regarding destruction

    Please have a look at below sample code:
    Code:
    #include<iostream.h>
    class Test
    {
     public:
       void fun()
       {
           cout<<"In fun"<<endl;
       }
       Test()
       {
           cout<<"In const"<<endl;
       }
       ~Test()
       {
           cout<<"In dest"<<endl;
       }
    };
    int main()
    {
        cout<<"Hello"<<endl;
        Test * t = new Test();
        t->fun();
        delete t;
        t->fun();
        return 0;
    }
    In above I created object "t" and destroyed using delete operator.
    After destroying also I am able to call the function "fun".How it is possible
    output of above code :
    Hello
    In const
    In fun
    In dest
    In fun

    If i am able to call the function , than what actually destruction will do ?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Regarding destruction

    Sure, You are not accessing any *DATA* in the destructed object.

    btw: This is still really a BAD practice.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Regarding destruction

    Quote Originally Posted by mfdiqwer
    In above I created object "t" and destroyed using delete operator. After destroying also I am able to call the function "fun".How it is possible
    When an object's destructor is called, the object is no longer valid to use. This means you shouldn't be using it -- using it will exhibit undefined behaviour. This undefined behaviour could mean anything, including seemingly work correctly, or crash right away.
    If i am able to call the function , than what actually destruction will do ?
    Destruction doesn't mean to wipe it away from your program forever. All it means is that the object is no longer valid to use.

    Just as if you are driving with an expired driver's license -- just because you have an expired license doesn't mean you can't physically get in a car and drive it around. Sure, you might be stopped by the police at some point, or you can drive for a 1,000 miles without any problems -- still, it's illegal to drive with an expired license.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Regarding destruction

    Ha! I like your analogy, Paul!

    Viggy

  5. #5
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Regarding destruction

    it is undefined behavior...
    but should run on all compilers...

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Regarding destruction

    Quote Originally Posted by Mitsukai
    it is undefined behavior...
    but should run on all compilers...
    Correction: It should run MOST of the time on MOST common compilers.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Regarding destruction

    I know of no compilers where non-virtual methods that do not use any instance data actually become invalid.

    That being said, it is still undefined.

    As a side note, there are emerging "standards" which state that member functions which do not reference any instance data should be declared static, since they are not really specific to the instance.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Regarding destruction

    Quote Originally Posted by TheCPUWizard
    As a side note, there are emerging "standards" which state that member functions which do not reference any instance data should be declared static, since they are not really specific to the instance.....
    Bad idea... There are cases where member functions happen, as an implementation detail subject to change, not to use any non-static member, but it doesn't mean that you've to declare this function as static because it's likely to evolve in future. Actually, there are even cases where we write virtual functions accessing no member!
    But I guess that designing with a brain is harder than designing with stupid automatisms.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  9. #9
    Join Date
    Oct 2005
    Posts
    199

    Re: Regarding destruction

    You should set the pointer of object to 0 after deletion.
    Code:
    delete t;
    t = 0;
    In addition, you should check if the pointer exists before calling methods
    Code:
    if (t)
        t->fun();
    'You help me, and I, in turn, am helped by you!'
    -H.S.

  10. #10
    Join Date
    Feb 2007
    Posts
    141

    Re: Regarding destruction

    HI Paul McKenzie

    u had presentated nice example to explain destructor.

    but what i read is that destructor frees the memory of that object so that we can resuse that memory for other purpose.

    u tell that if driver's license expired then he can no longer use it legaly.
    but what i think is that destructor means to destroy the users license so that he cannot use it anymore in future.

    Paul McKenzie can u explain it clearly what exactly destructor do with an object.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured