There are two ways: simple cheap and right. Two answers from @Naveen and @Nawaz concern the correct one, requiring the manual creation of a sealant class for each class that you really want to seal.
The invalid method used by adobe libraries uses a template class for this. The problem is that you cannot declare the template argument as a friend, which means that you need to switch from private to less secure protected :
template <typename T> class sealer { protected: sealer() {} }; class sealed : virtual sealer<sealed> {};
And you can automate it with a macro (I don’t remember the exact taste of the macro in Adobe code):
#define seal( x ) virtual sealer<x> class sealed : seal(sealed) {};
Now this will catch people who mistakenly try to inherit, not knowing that they should not:
class derived : sealed {}; int main() { derived d;
But it will not prevent people who really want to extract from them, since they can access the constructor based on the templates themselves:
class derived : sealed, sealer<sealed> {}; int main() { derived d; };
I'm not sure if this will change in C ++ 0x, I think I recall some discussions about whether the class template is allowed to make friends with one of its arguments, but in a cursory search on the project I can’t say If it were allowed, this would be a great general solution:
template <typename T> class sealer { sealer() {} friend class T;
David Rodríguez - dribeas Jan 17 2018-11-12T00: 00Z
source share