Are there any circumstances in which it is legitimate for a derived class to have a non-virtual
destructor? A non-virtual
destructor signifies that a class should not be used as a base-class. Will having a non-virtual
destructor of a derived class act like a weak form of the Java final
modifier?
I am especially interested in the case where the base class of the derived class has a virtual
destructor.
Note: in a derived class, if your base class has a virtual destructor, your own destructor is automatically virtual. You might need an explicitly defined destructor for other reasons, but there's no need to redeclare a destructor simply to make sure it is virtual.
No. You never need to explicitly call a destructor (except with placement new). A derived class's destructor (whether or not you explicitly define one) automagically invokes the destructors for base class subobjects. Base classes are destructed after member objects.
Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor.
Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.
Are there any circumstances in which it is legitimate for a derived class to have a non-virtual destructor?
Yes.
A non-virtual destructor signifies that a class should not be used as a base-class.
Not really; a non-virtual destructor signifies that deleting an instance of derived
via a base
pointer will not work. For example:
class Base {}; class Derived : public Base {}; Base* b = new Derived; delete b; // Does not call Derived's destructor!
If you don't do delete
in the above manner, then it will be fine. But if that's the case, then you would probably be using composition and not inheritance.
Will having a non-virtual destructor of a derived class act like a weak form of the Java final modifier?
No, because virtual
-ness propagates to derived classes.
class Base { public: virtual ~Base() {} virtual void Foo() {}; }; class Derived : public Base { public: ~Derived() {} // Will also be virtual void Foo() {}; // Will also be virtual };
There isn't a built-in language mechanism in C++03 or earlier to prevent subclasses(*). Which isn't much of an issue anyway since you should always prefer composition over inheritance. That is, use inheritance when a "is-a" relationship makes more sense than a true "has-a" relationship.
(*) 'final' modifier was introduced in C++11
It is perfectly valid to have an Base class with an non virtual destructor if you are never going to call delete on a Base class pointer pointing to an derived class object.
Follow Herb Sutter's Advice:
Guideline #: Only if derived classes need to invoke the base implementation of a virtual function, make the virtual function protected. For the special case of the destructor only:
Guideline #: A base class destructor should be either public and virtual, or protected and nonvirtual.
Maybe your question actually is:
Does Destructor in Derived class needs to be virtual if Base class Destructor is virtual?
The answer is NO.
If Base class destructor is virtual then the Derived class destructor is implicitly virtual already, you don't need to specify it as virtual explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With