继承构造函数
构造函数“透传”
struct A {
A(int i) {}
A(double d, int i) {}
A(float f, int i, const char* c) {}
};
struct B: public A{
B(int i): A(i) {}
B(double d, int i) : A(d,i){}
B(float f, int i, const char* c) :A(f,ic,c){}
virtual void ExtraInterface() {}
};
继承构造函数
struct A {
A(int i) {}
A(double d, int i) {}
A(float f, int i, const char* c) {}
};
struct B: public A{
using A::A;
virtual void ExtraInterface() {}
};
注意:基类构造函数的参数有默认值时,默认值是不会被继承的,默认值会导致基类产生多个构造函数的版本,这些函数版本都会被派生类继承。
struct A {
A(int a = 3, double = 2.4){}
};
struct B: A{
using A::A;
};
1. A(int =3, double =2.4);
2. A(int =3);
3. A(const A&);
4. A();
1. B(int, double);
2. B(int);
3. B(const B&);
4. B();
继承构造函数“冲突”
struct A { A(int) {}};
struct B { B(int) {}};
struct C:A,B{
using A::A;
using B::B;
};
struct C:A,B{
using A::A;
using B::B;
C(int){}
};
|