Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a Derived Class to use the Constructor of the Base Class

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?

like image 529
danijar Avatar asked Oct 08 '12 14:10

danijar


People also ask

How can a base class constructor be used as a derived class?

How to call the parameterized constructor of base class in derived class constructor? To call the parameterized constructor of base class when derived class's parameterized constructor is called, you have to explicitly specify the base class's parameterized constructor in derived class as shown in below program: C++

Can a derived class inherit the constructor of a base class?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.

Does a derived class use base constructor C++?

Any class that's derived from Abstract must use the one and only constructor in Abstract . Just don't create any constructors that don't do what you want. Since the derived class instances are instances of the base class, a constructor must get called. There's nothing special you need to do.

Can you assign a derived class object to a base class object?

Object Slicing in C++ In C++, a derived class object can be assigned to a base class object, but the other way is not possible.


2 Answers

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:

class Base
{
    Base(int Member, string Text) { };
};

class Derived : public Base
{
    using Base::Base; // <-- Brings to derived the Base's constructor.
};

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.

like image 89
PaperBirdMaster Avatar answered Sep 20 '22 09:09

PaperBirdMaster


Use initializer list of the derived class' constructor.

class Base
{
    Base(int Member, string Text) { //...
    }
};

class Derived : public Base
{
    Derived(int Member, string Text) : Base(Member, Text) {
                                    // ^^^^^^^^^^^^^^^^^^
        // ...
    }
};
like image 25
timrau Avatar answered Sep 21 '22 09:09

timrau