Is there a way to force a derived class to use the constructor of the abstract base class? It must not be a real constructor, I have an open mind about creative solutions.
class Abstract
{
private:
int Member;
string Text;
public:
Abstract(int Member, string Text)
{
this->Member = Member;
this->Text = Text;
}
// e.g. defining virtual functions
}
For example my abstract class has some private members which every derived class should also have. And they should be defined in the constructor, even against the will of the derived class.
I am aware that constructors are not inherited. But is there a workaround to produce a similar behavior?
As suggested by other users, you must call the base class constructor into the initializer list of derived class constructor.
But there's another cool solution bringed up by C++11: the inherited constructors:
But you must assure that your compiler can use C++11 features; and of course, study if the inherited constructor conforms to your requirements instead of using it just because it's cool.
Use initializer list of the derived class' constructor.