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

    virtual destructor

    I know a virtual function is a member function of a class whose functionality can be overridden in its derived class.

    If you have a virtual statement, do you need to assign your destructor as a virtual? If so, or if not why?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: virtual destructor

    Quote Originally Posted by math8 View Post
    I know a virtual function is a member function of a class whose functionality can be overridden in its derived class.

    If you have a virtual statement, do you need to assign your destructor as a virtual? If so, or if not why?
    What do you mean "If you have a virtual statement"? Destructors should be virtual so that derived classes can clean up after themselves properly.

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    Re: virtual destructor

    If you have a class with virtual function then the virtual destructor is required. Otherwise normal destructor is sufficient.

  4. #4
    Join Date
    Feb 2013
    Posts
    36

    Re: virtual destructor

    Thanks, that makes sense

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: virtual destructor

    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual destructor

    Quote Originally Posted by Rajesh1978 View Post
    If you have a class with virtual function then the virtual destructor is required. Otherwise normal destructor is sufficient.
    That's a common rule of thumb but it's a little bit too limiting (and it's not quite accurate).

    You only need a virtual destructor in a base class if you plan to use delete on a base class pointer that in fact holds a pointer to a derived class object (a polymorphic delete). If you only ever delete derived class pointers directly then the base class destructor needs not be virtual. To enforce that you could make the base class destructor protected.

    So a more complete rule of thumb would be: Make base class destructors public and virtual, or protected and nonvirtual. (See item 50 in C++ Coding Standards by Sutter & Alexandrescu for a discussion.)
    Last edited by nuzzle; March 7th, 2013 at 10:01 AM.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: virtual destructor

    Quote Originally Posted by Rajesh1978 View Post
    If you have a class with virtual function then the virtual destructor is required. Otherwise normal destructor is sufficient.
    This isn't correct, the 2 are unrelated.

    the only NEED for a virtual destructor is if you plan to delete a derived object through a base pointer AND this derived class has a need for additional destructor vode or has additional data members that have destructors that need to be called.

    it is entirely possible to have a class with no virtual functions at all, but still needing a virtual destructor to function properly.

    I will "agree" to the fact that a class which has virtual functions, it is "probably a good idea" to make the destructor virtual also.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: virtual destructor

    Quote Originally Posted by OReubens
    the only NEED for a virtual destructor is if you plan to delete a derived object through a base pointer AND this derived class has a need for additional destructor vode or has additional data members that have destructors that need to be called.
    The subclause following the "AND" should be discarded.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: virtual destructor

    Quote Originally Posted by laserlight View Post
    The subclause following the "AND" should be discarded.
    Hmm not quite...
    If the derived class does not have additional datamembers, and doesn't do anything to existing datamembers (or globals) that need to be reversed, then the derived class doesn't need a destructor at all.

    I'm not sure if this is 100% "by the book", but the above rule works for every single c++ compiler (even some very oddball ones) on every single platform tested so far.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: virtual destructor

    Quote Originally Posted by OReubens View Post
    Hmm not quite...
    If the derived class does not have additional datamembers, and doesn't do anything to existing datamembers (or globals) that need to be reversed, then the derived class doesn't need a destructor at all.

    I'm not sure if this is 100% "by the book", but the above rule works for every single c++ compiler (even some very oddball ones) on every single platform tested so far.
    That assumes that the designer of the base class knows the implementations of the classes that will be derived from it, and that's a bad assumption to make. There's no reason not to make a destructor virtual.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: virtual destructor

    Quote Originally Posted by OReubens
    If the derived class does not have additional datamembers, and doesn't do anything to existing datamembers (or globals) that need to be reversed, then the derived class doesn't need a destructor at all.

    I'm not sure if this is 100% "by the book", but the above rule works for every single c++ compiler (even some very oddball ones) on every single platform tested so far.
    The 100% by the book rule says that there is undefined behaviour, even if it "works for every single c++ compiler" in existence and in history. But yeah, practically speaking, I believe that you are right. However, because of the theoretical undefined behaviour, the fact that the "100% by the book" rule is so much simpler and hence easier to remember, and the point that GCDEF raised, the subclause following the "AND" should be discarded.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Oct 2008
    Posts
    1,456

    Re: virtual destructor

    In any case, your sentence should be eventually corrected to

    the only NEED for a virtual destructor is if you plan to delete a derived object through a base pointer AND this derived class has a need for additional destructor code or deallocation functions ( including the base itself ) or has additional data members or base classes that have destructors that need to be called.

    BTW, note that a recent proposal asked for an optionally sized global deallocation function in order to support efficient size-aware allocation strategies; if accepted, it would break such illegal code ( the deallocation function would not be given the correct size of the object to delete ).

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