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.
Yes.
Not really; a non-virtual destructor signifies that deleting an instance of
derivedvia abasepointer will not work. For example:If you don’t do
deletein the above manner, then it will be fine. But if that’s the case, then you would probably be using composition and not inheritance.No, because
virtual-ness propagates to derived classes.Here is the excerpt from the C++11 standard that formalizes this:
– [class.dtor] p9
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