Search code examples
c++inheritancec++14class-constructors

Can I make subclasses implicitly inherit base class constructors?


Suppose I have:

class A {
public:
        A(int x_) : x(x_) {}
        int x;
};

class B: public A { };
class C: public A { };

With this code, B and C won't have any constructors (other than the copy constructor). I would like to change something in class A (not in B or C) so that both B and C will inherit the constructor of A. Is that possible somehow?


Solution

  • It is not possible to have them implicitly. You can explicitly have the constructors available via:

    class B: public A { using A::A; };
    class C: public A { using A::A; };