Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance from empty base class in C++

I want to make an empty base class called "Node", and then have other classes derived from this such as "DecisionNode" and "Leaf." It makes sense to do this so I can take advantage of polymorphism to pass these different kinds of nodes to methods without knowing at compile time what will be passed to the method, but each of the derived classes do not share any state or methods.

I thought the best way to implement this, without creating an additional pure virtual method in the base class, which would add clutter, would be to make the constructor pure virtual. In the header file for the class, "Node.h" I therefore wrote:

class Node {
 private:
  virtual Node();
};

and in "Node.cpp" I wrote:

#include "Node.h"
virtual Node::Node() = 0;

This implementation prevents Node from ever being instantiated by another class, since the only constructor is private and uses the pure virtual specifier to indicate that the class is abstract. However, this gives the 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 make an empty abstract base class?

like image 578
Froskoy Avatar asked Dec 21 '12 11:12

Froskoy


People also ask

What can be inherited by a class from a base class?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

Can base classes inherit from another base class?

Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

Can we inherit empty class in C++?

In C++ you have Multiple Inheritance so there is literally no benefit to having those empty classes.

Is it possible for a class to inherit the constructors of its base class in C++?

Multiple Inheritance is a feature of C++ where a class can derive from several(two or more) base classes. The constructors of inherited classes are called in the same order in which they are inherited.


2 Answers

C++ doesn't support virtual constructor.

§ 12.1 Constructors

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

Below code won't compile:

virtual Node::Node() = 0;

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

Yes, make destructor a pure virtual function, also provides destructor function definition

class Node 
{
public:
    virtual ~Node()=0
    {
    }
};
like image 143
billz Avatar answered Sep 26 '22 01:09

billz


you can't make the constructor virtual. If no other pure virtual functions are needed you can make the destructor pure virtual:

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

Node::~Node()
{
  // Compulsory virtual destructor definition,
  // even if it's empty
}
like image 22
Marius Avatar answered Sep 24 '22 01:09

Marius