Everyone knows that the desructor of base class usually has to be virtual. But what is about the destructor of derived class? In C++11 we have keyword \"override\" and ability t
According to the CppCoreGuidelines C.128 the destructor of the derived class should not be declared virtual
or override
.
If a base class destructor is declared virtual, one should avoid declaring derived class destructors
virtual
oroverride
. Some code base and tools might insist on override for destructors, but that is not the recommendation of these guidelines.
UPDATE: To answer the question why we have a special case for destructors.
Method overriding is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.
In other words, when you call an overridden method only the last implementation of that method (in the class hierarchy) is actually executed while all the destructors (from the last child to the root parent) must be called to properly release all the resources owned by the object.
Thus we don't really replace (override) the destructor, we add additional one into the chain of object destructors.
UPDATE: This CppCoreGuidelines C.128 rule was changed (by 1448, 1446 issues) in an effort to simplify already exhaustive list of exceptions. So the general rule can be summarized as:
For class users, all virtual functions including destructors are equally polymorphic.
Marking destructors
override
on state-owning subclasses is textbook hygiene that you should all be doing by routine (ref.).