I just found a very old article of mine, which got published in Dr. Dobb's Journal, the Tech Tips section, on October 1, 2003! The title of the article was Preventing Class Derivation in Visual C++ .NET. Either see that link, or read on if you're interested.
Preventing Class Derivation in Visual C++ .NET
Here is a tip for preventing derivation from a class in C++. Suppose you have a class B, and you don't want any class to derive from this class. C++, unlike other languages such as Java, doesn't have any built-in support for this task, but this can be done using virtual inheritance. You should create and use a Lock class as this example shows:
class Lock { Lock() {} friend class B; }; class B : private virtual Lock { public: B() {} }; class D : public B { public: D() {} }; int main() { B b; D d; return 0; }
B is a friend of Lock, so B::B() has access to Lock::Lock() and can initialize the virtual base class. On the other hand, D is not a friend of B, so D::D() can't initialize the virtual base class due to its inability to call Lock::Lock(). Thus, attempting to compile the aforementioned program will lead to an error. Microsoft Visual C++ .NET 2003 produces the following error messages when compiling this application:
h:\Projects\VC++.NET\test\test\test\test.cpp(17): error C2248: 'Lock::Lock' : cannot access private member declared in class 'Lock'
h:\Projects\VC++.NET\test\test\test\test.cpp(3): see declaration of 'Lock::Lock'
h:\Projects\VC++.NET\test\test\test\test.cpp(2): see declaration of 'Lock'
h:\Projects\VC++.NET\test\test\test\test.cpp(22): error C2248: 'Lock::Lock' : cannot access private member declared in class 'Lock'
h:\Projects\VC++.NET\test\test\test\test.cpp(3): see declaration of 'Lock::Lock'
h:\Projects\VC++.NET\test\test\test\test.cpp(2): see declaration of 'Lock'