Hi Markus,
In Java you have a keyword which is called final, in C# it is sealed. If the classes are declared with this words you can not inherit from them. In C++ we do not have this possibility. But if you derived from a class which does not have a virtual destructor, the class is not designed for derivation. With other words you should use it as a base class. This is the same with classes from stl (vector etc.)
Actually, I think that this is not the very same. I agree on what you’ve written, but there are situations when you have a non-virtual dtor in a base class. If you don’t want users to derive from your class (in C++), you can use one of the options suggested here. The explanation why use a virtual dtor in your base classes is written here.
So, now to what I wanted to say. There are basicly two options you can use when defining a base class:
- Use a public and virtual dtor - this is mostly what you want if you want to allow users to call a delete operator on a derived-class object via a base-class pointer.
- Use a protected and non-virtual dtor - this is used when you don’t want to allow users to call a delete operator on a derived-class object via a base-class pointer. An example could be policy classes (see Modern C++ Design by A. Alexandrescu (book) for more details).
Reference: C++ Coding Standards, rule n.50 (book)
Regards,
Petr
