I want to create an empty base class called "Node", and then get other classes from it, such as "DecisionNode" and "Leaf". It makes sense to do this so that I can use polymorphism to pass these various types of nodes to methods, not knowing at compile time what will be passed to the method, but each of the derived classes does not have a common state or methods.
I thought that the best way to implement this without creating an additional pure virtual method in the base class that would add clutter was to make the constructor pure virtual. In the file header for the "Node.h" class, I therefore wrote:
class Node { private: virtual Node(); };
and in Node.cpp "I wrote:
#include "Node.h" virtual Node::Node() = 0;
This implementation prevents the creation of a Node instance by another class, since the only constructor is private and uses a pure virtual qualifier to indicate that the class is abstract. However, this gives compiler errors:
Node.h:6:21: error: return type specification for constructor invalid Node.h:6:21: error: constructors cannot be declared virtual [-fpermissive]
My question is: is there a neat way to create an empty abstract base class?
source share