Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a purely virtual/abstract class require a constructor, in particular for protected const member variables?

I have a purely virtual class defined as such:

class BaseClass {
 protected:
  const int var;
 public:
  void somefun() = 0; // what I mean by a purely virtual class
  // stuff...
};

If I don't add a constructor defined as such:

BaseClass(const int & VAR) : var(VAR) {};

that I would have to subsequently use in ever derived class, my derived class can't initialize the const variable var to whichever value it wants to. Now I actually understand what's going on here. Before constructing a derived class, a constructor of the base class is called, at which point const member variables must be initialized. My question is not a "how do I make my code work" kind of question, that's already been done. My question is about why the compiler thinks it's necessary. For a purely virtual class, shouldn't I be allowed to write something like:

class DerivedClass : BaseClass {
 public:
  DerivedClass() : var(SOME_VALUE) {};
}

If the compiler knows that a call to a BaseClass constructor will necessarily be followed by a call to some derived class constructror (since an object of abstract type can never be instantiated) shouldn't it give us a bit more leeway?

Is this all a consequence of how C++ chooses to get around the Diamond problem? Even if that was the case, shouldn't the compiler at least somehow allow for the possibility that const member variable of purely virtual functions will be defined in derived classes? Is that too complicated or does that mess with the C++ solution to the Diamond problem?

Thanks for the help everyone.

like image 825
ticster Avatar asked Feb 22 '12 16:02

ticster


People also ask

Do pure virtual classes need a constructor?

A pure virtual class cannot be instantiated, so it doesn't make a difference if the constructor is public or protected. A public constructor is syntactically correct. However, making it protected will carry a stronger indication that the class cannot be instantiated. Save this answer.

What is the purpose of a constructor in an abstract class C++?

The main purpose of the constructor is to initialize the newly created object. In abstract class, we have an instance variable, abstract methods, and non-abstract methods. We need to initialize the non-abstract methods and instance variables, therefore abstract classes have a constructor.

Why do pure virtual destructors have to have a body?

Why a pure virtual function requires a function body? The reason is that destructors (unlike other functions) are not actually 'overridden', rather they are always called in the reverse order of the class derivation.

Should abstract base class have constructor?

Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor.


2 Answers

It's not "purely virtual" (whatever you mean by that) - it contains a data member.

Class members can only be initialised by the initialiser list of a constructor of that class, not of a derived class. That's how object initialisation is specified: all members that are initialised, are initialised before the constructor body begins.

Constant objects must be initialised, since they can't be assigned a value later.

Therefore, a class with a constant data member must initialise it in each constructor.

like image 121
Mike Seymour Avatar answered Sep 20 '22 23:09

Mike Seymour


For a purely virtual class, shouldn't I be allowed to write something like

No, but you can(and in this case should) write something like this:

class DerivedClass : BaseClass {
 public:
  DerivedClass() : BaseClass(SOME_VALUE) {};
};
like image 43
mfontanini Avatar answered Sep 20 '22 23:09

mfontanini