C++11 - Inherited constructors

less than 1 minute read

Now C++11 allow that derived class can inherit the constructors of base class as the following.

1
2
3
4
5
6
7
8
9
10
11
class A
{
public:
    A(int a) {}
    A(std::string const& str) {}
    A(double a, int f) {}
};
class B : public A
{
    using base::A;
};

However, you need to be careful when if B has any member variable, they will be initialized with random values unless you initialize specifically.