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.
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;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
pure virtual methods:
Of course it must be implemented in a subclass.Code:class X { X(); ~X(); virtual void PureVirtualMethod() = 0; };
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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.