Can I make subclasses implicitly inherit base class constructors?

66 Views Asked by At

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?

1

There are 1 best solutions below

4
On BEST ANSWER

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; };