First of all, if you create a virtual base class destructor, all derived classes will automatically receive a virtual destructor if you declare them virtual or not. This is usually true for signature matching: if the base class has a virtual function with the same signature as the function in the derived class, the function in the derived class is override and has the value virtual (although in C ++ 2011 you can prevent further overriding using the key final , in which case another redefinition will cause an error).
However, destructors are special: when you create a virtual destructor, it will still be called, even if there is another main deregulator! The only hit of the destructor that is virtual is what happens if you delete an object using a pointer to the base class when the object is actually of a derived type: if the destructor is not virtual , you get undefined while Right Thing happens. if the destructor is virtual . For instance:
class not_a_base {}; class bad_idea: public not_a_base {}; class a_base { public: virtual ~a_base() {} }; class ok: public a_base {}; int main() { a_base* ab = new ok; delete ab;
The reason destructors are not virtual by default simply means that this will mean that the size of the object always increases by the size of the word, which is generally unacceptable.
source share