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.
This is a discussion on How can I make the base class unable to instantiate? within the C++ Programming forums, part of the General Programming Boards category; For instance, classes A, B, and C derive from base class X. How can I make sure it's only A, ...
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;
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.