Thread: virtual destructor

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    virtual destructor

    Hello everyone,


    What is the purpose of virtual destructor? If currently, no derived class?


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To allow for derived classes in the future to have objects that can be safely deleted polymorphically through a pointer to this base class. Or, to put it in another way, to show that this class is designed to be a base class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Does that mean if you dont define virtual destructor - destructor wont be called in derived class?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A base class's destructor will not be called when the derived class is destructed if it (the base's destructor) is not virtual.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It will be called, unless you happen to delete it via a base class pointer, in which case it would be, uh, partially destroyed, or some other strange thing like that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks hk_mp5kpdw,


    Quote Originally Posted by hk_mp5kpdw View Post
    A base class's destructor will not be called when the derived class is destructed if it (the base's destructor) is not virtual.
    Is there any conclusion whether non-virtual destructor will be called if,

    1. through derived class's pointer;
    2. through base 's pointer.


    regards,
    George

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is always a call to SOME destructor when an object is deleted [even if you don't even have a destructor in your class - in which case a default destructor is defined by the compiler - it may even optimize the then empty destructor out, as it doesn't really do anything].

    The difference between a virtual and a non-virtual destructor is the determination of WHICH destructor to call. If you have this code:
    Code:
    class base
    {
     ... 
    };
    
    class derived: public base
    {
    ...
    };
    
    base *bp = new derived;
    derived *dp = new derived;
    
    ...
       delete *bp; // One 
    ...
       delete *dp; // Two 
    If we assume that the destructor is non-virtual, then the One will call the base destructor - because the pointer is of class base. The Two will call the derived destructor.

    If we change the base destructor by adding virtual, then the destructor indicated by the object's vtable will be called, in this case both One and Two will call the derived destructor - which of course is what you want, since derived may need more "destruction" than the base.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you delete an object of a derived class through a pointer to that derived class, its destructor will certainly be called. This destructor will then invoke the base class destructor, so all is well.

    If you delete an object of a derived class through a pointer to its base class, and the base class destructor is not virtual, we could reason that the base class destructor will be called, but not the derived class destructor. The behaviour in such a case is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    If you delete an object of a derived class through a pointer to its base class, and the base class destructor is not virtual, we could reason that the base class destructor will be called, but not the derived class destructor. The behaviour in such a case is undefined.
    I thought this was well-defined: the base-classes destructor is called. Or did you mean that "calling the base classes destructor when the actual object isn't of that type is undefined".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I thought this was well-defined: the base-classes destructor is called. Or did you mean that "calling the base classes destructor when the actual object isn't of that type is undefined".
    Of course, since I already stated that we are dealing with "an object of a derived class through a pointer to its base class". Actually, I think that objects of a derived class are objects of the base class, but the normal polymorphic substitution fails here for deletion if the base class destructor is not virtual.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Of course, since I already stated that we are dealing with "an object of a derived class through a pointer to its base class". Actually, I think that objects of a derived class are objects of the base class, but the normal polymorphic substitution fails here for deletion if the base class destructor is not virtual.
    Yes, I'm just trying to understand what part is "undefined". The derived class will always contain the base-class, so it should be safe to call the base-classes destructor, everything should be there. But as you say, the destruction of anything that is specific to the derived class would not happen, and that's obviously bad.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you delete a base class pointer that points to a derived class, then the behavior is undefined (and apparently in practice what usually happens is that the derived class destructor is not called).
    Code:
    class base
    {
    public:
      ~base() { }
    };
    
    class derived : public base
    {
    };
    
    int main()
    {
      base* bp = new Derived;
      delete bp;
    }
    The red code causes undefined behavior.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'd say, if you really want to know what gets called when, create a base & derived class that print a message in their destructors, then play around with different scenarios (virtual, non-virtual, pointer to base, pointer to derived...).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. A virtual destructor dilema
    By tzakieta in forum C++ Programming
    Replies: 10
    Last Post: 09-11-2008, 09:22 AM
  3. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 11:08 AM
  4. virtual destructor problem
    By sunnypalsingh in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2005, 05:07 AM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM