C++11 introduced the final
keyword to C++.
It can be used on a virtual method or on a class.
Declaring a class final forbids any kind of inheritance: public, protected and private.
struct A final {
};
class B: private A {
};
error: base 'A' ^ is marked 'final'
While it can be reasonable to forbid public inheritance (e.g. if my class doesn't have a virtual destructor, or for other reasons), why should I forbid private inheritance?
Might it be that if final
forbade only public inheritance, that std::string
and its other friends in std would have been final
-- as they should -- for not having a virtual destructor?
EDIT:
Howard Hinnant already answered Why the standard containers are not final but still, there is a reason for declaring a class final but allowing private inheritance.