struct B {
B () {}
B(int i) {}
};
struct D : B {
using B::B; // <--- new C++11 feature
};
D d1; // ok
D d2(3); // ok
Now, if I add a new constructor inside the body of struct D
, such as:
struct D : B {
using B::B;
D(const char* pc) {} // <--- added
};
then D d1;
starts giving compiler error(ideone is not upgraded yet, I am using g++ 4.8.0)? However D d2(3);
still works.
Why the default constructor is discounted when adding a new constructor inside struct D
?
There is a subtle difference between
struct D : B {
using B::B;
D(const char* pc) {} // <--- added
};
versus
struct D : B {
using B::B;
};
In the second case, compiler auto-generate the default "D(){}" constructor for you. But if you create your own constructor for D, then the default "D(){}" is not available anymore. Sure you have inherited B's default constructor, but that doesn't tell the compiler how to construct D by default.