Automatically executing a constructor that invokes a specific base class constructor

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 a way to accomplish what the above program is looking for (so that B has a constructor with the same parameter as the base class)? Is this the correct syntax?

If so, is this a C ++ 11/14 function, or can it be done in C ++ 03?

+4
source share
2 answers

Is there a way to accomplish what the above program is looking for (so that B has a constructor with the same parameter as the base class)?

Yes there is. Using inheriting constructors:

using A::A;

Is this the correct syntax for it?

Yes.

If so, is this a C ++ 11/14 function, or can it be done in C ++ 03?

This function was introduced in C ++ 11. This is not valid in C ++ 03.

. .

+6

, ( ):

struct A
{
    A(int a) {}
};

struct B : A
{
    using A::A; // Shorthand for B(int b) : A(b) {}?
};

int main()
{
    B b(3);
}

( )

++ 11.

+3

All Articles