4
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?

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Bwmat
  • 4,314
  • 3
  • 27
  • 42

2 Answers2

6

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 5
    Note that this compiles in VS2013 but doesn't actually do anything ([VS14 finally got around to implementing them](http://blogs.msdn.com/b/vcblog/archive/2014/06/11/c-11-14-feature-tables-for-visual-studio-14-ctp1.aspx)). – Cameron Jan 16 '15 at 19:29
  • haha, I would need this to work in VS2010/12/13, not to mention some old HP-UX/AIX compilers, so I guess it's a no go for me. – Bwmat Jan 16 '15 at 20:39
3

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055