class A
{
A(int a);
};
class B : public A
{
using A::A; // Shorthand for B(int b) : A(b) {}?
};
int main()
{
B b(3);
return 0;
}
Is there some way to accomplish what the above program seeks to (to make B have a constructor with the same parameter's as a base class')? Is that the correct syntax for it?
If so, is it a C++11/14 feature, or can it be done in C++03?
Is there some way to accomplish what the above program seeks to (to make B have a constructor with the same parameter's as a base class')?
Yes, there is. Using inheriting constructors:
using A::A;
Is that the correct syntax for it?
Yes.
If so, is it a C++11/14 feature, or can it be done in C++03?
This feature was introduced in C++11. It is not valid in C++03.
For more information, see the relevant section of this using declaration reference.
Yes, exactly like that (once I cleaned up your unrelated errors):
struct A
{
A(int a) {}
};
struct B : A
{
using A::A; // Shorthand for B(int b) : A(b) {}?
};
int main()
{
B b(3);
}
(live demo)
These are called inheriting constructors, and are new since C++11.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With