Search This Blog

Sunday, November 15, 2009

C++, Seal Your Base Class, non-inheritable

Like the keyword seal on class in c#, in C++ we can just make the base class' constructor private. This way the derive class cannot instantiate itself because it has to call base's (the inaccessible) constructor first.

So how do we construct the base class this way? Simply provides a static function which calls the private constructor.

Note: keyword private provides the notion of accessibility, not visibility.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8

No comments: