#define SEALED(className) \
className ## Sealer \
{ \
private: className ## Sealer(){}; \
friend class className; \
}; \
class className : virtual private className ## Sealer
class SEALED(MyClass) {};
class MyClassDisallowed : public MyClass {};
int main()
{
// Perfectly legal construction
MyClass myClass;
// Illegal construction, super-class is sealed
MyClassDisallowed myClassDisallowed;
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
http://www.codeproject.com/KB/cpp/SealingCppClasses.aspx
Author
Commented:With the template way of doing this all the sealer classes are defined within the namespace of the template. Although I generally advocate not using preprocessor macros, in this instance it means the sealer class shares the name of the class being sealed and the sealer class will be in the same namespace.