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?
Printable View
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?
If you have a class with virtual function then the virtual destructor is required. Otherwise normal destructor is sufficient.
Thanks, that makes sense
See http://forums.codeguru.com/showthrea...view-Questions for a discussion about this.
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.)
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.
The subclause following the "AND" should be discarded.Quote:
Originally Posted by OReubens
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.
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.Quote:
Originally Posted by OReubens
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 ).