Inheritance from an empty base class in C ++

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?

+4
source share
6 answers

C ++ does not support virtual constructor.

Β§ 12.1 Constructors

12.1.4 The constructor shall not be virtual (10.3) or static (9.4).

Below code will not compile:

 virtual Node::Node() = 0; 

My question is: is there a neat way to create an empty abstract base class?

Yes, making the destructor a pure virtual function also provides a definition of the destructor function

 class Node { public: virtual ~Node()=0 { } }; 
+5
source

you cannot make the constructor virtual. If other pure virtual functions are not needed, you can make the destructor pure virtual:

 class Node { public: virtual ~Node() = 0; }; Node::~Node() { // Compulsory virtual destructor definition, // even if it empty } 
+7
source

Create a virtual destructor and also provide an "empty" implementation.

 class Node { virtual ~Node() = 0; } Node::~Node() {} // You will get linker error if you do not have this 

Another option is to make the constructor secure, as others have commented. See also this question for some differences between the two. protected constructor versus pure virtual destructor

Edit Make sure you document why you are using a pure virtual destructor. The code itself is mysterious in this regard and does not make it clear to someone who does not know about this "trick".

Change 2 . Your constructor should be protected , not private . You cannot inherit if your constructor is private .

+4
source

In C ++, constructors cannot be virtual . To prevent someone from creating an instance of the base class, give them a protected constructor, for example:

 class Node { protected: Node() {} }; 

It will not be abstract, but only derived classes will be able to create their own instances.

0
source

Just:

 class Node { protected: Node() { } public: virtual ~Node() { } } 
0
source

What are you trying to do is

 class Node { private: virtual Node(); }; and in "Node.cpp" I wrote: #include "Node.h" // This is the error as your class name and function name ie same so compiler assumes // that this as a constructor and as per the c++ standard a constructor can not have // return type as well as can not be virtual virtual Node::Node() = 0; 

So you create a virtual distructor as ** virtual ~ Node () = 0; **

0
source

All Articles