5

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?

gherkins
  • 14,603
  • 6
  • 44
  • 70
danijar
  • 32,406
  • 45
  • 166
  • 297
  • 3
    Not defining default constructor forces derived classes to use any other constructor. – PiotrNycz Oct 08 '12 at 14:21
  • 2
    It's not clear what the question is. Any class that's derived from `Abstract` must use the one and only constructor in `Abstract`. – Pete Becker Oct 08 '12 at 14:25
  • 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. – David Schwartz Oct 08 '12 at 14:29

2 Answers2

19

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.

Zach
  • 1,263
  • 11
  • 25
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
9

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) {
                                    // ^^^^^^^^^^^^^^^^^^
        // ...
    }
};
timrau
  • 22,578
  • 4
  • 51
  • 64
  • 3
    This si a valid remark but I don’t think it answers OP’s question. – Konrad Rudolph Oct 08 '12 at 14:25
  • This approach came to my mind already, but it isn't what I am looking for. Because the derived class don't *have to* use the initializer list. Moreover it would be confusing with five or more parameters. – danijar Oct 08 '12 at 14:26
  • 3
    @KonradRudolph The question was "*Is there a way to force a derived class to use the constructor of the abstract base class?*" That sure enough forces the use of a Base-class constructor. – Robᵩ Oct 08 '12 at 14:26
  • @Robᵩ Well yeah, but so does OP’s code already. The point is that nothing in this answer explains that. See OP’s comment just above yours. – Konrad Rudolph Oct 08 '12 at 14:29
  • @sharethis - yes, the derived class does *have to* use the initializer list. If it doesn't, then no valid `Base` constructor is called, which is illegal. – Robᵩ Oct 08 '12 at 14:31