c++abstract-base-class

Can I define a constructor in an abstract base class?


I want to write an "interface" class in C++, which is a purely virtual abstract base class.

Can I define the constructors in this interface class? A constructor cannot be a purely virtual function, but how can I then define constructors for the interface class?

Edit: Do I need a virtual destructor in such an interface class?


Solution

  • There are in fact 2 questions in one:

    A constructor cannot be a purely virtual function

    TL/DR: if you are trying to add a constructor to your interface, then it is no longer an interface but a simple Abstract Base Class that is perfectly allowed to have one.