For instance, classes A, B, and C derive from base class X. How can I make sure it's only A, B, and C that can instantiate, and not X? Thanks in advanced.
Printable View
For instance, classes A, B, and C derive from base class X. How can I make sure it's only A, B, and C that can instantiate, and not X? Thanks in advanced.
Provide X with a pure virtual function, e.g. virtual ~X() = 0;
pure virtual methods:
Of course it must be implemented in a subclass.Code:class X
{
X();
~X();
virtual void PureVirtualMethod() = 0;
};
Making all constructors of X or its destructor protected also effectively prevents instantiation (except within X's member functions or its friends). That ability to instantiate only in defined places can be useful at times.
That should do the trick. Thanks.