虚拟基础的构造函数参数 类
Constructor arguments for virtual base classes
考虑以下代码:
class A {
int i;
public:
A(int index) : i(index) {}
int get() { return i; }
};
class B : virtual public A {
public:
using A::A;
};
class C : virtual public A {
public:
using A::A;
};
class D : public B, public C {
public:
D(int i) : A(i), B(i), C(i) {}
};
int main() {
D d(1);
return 0;
}
虽然 clang 3.7 接受上述内容,但带有 -std=c++11
的 gcc 4.8 会抱怨此代码:
In constructor 'D::D(int)':
20:29: error: use of deleted function 'B::B(int)'
D(int i) : A(i), B(i), C(i) {}
^
10:12: note: 'B::B(int)' is implicitly deleted because the default definition would be ill-formed:
using A::A;
^
10:12: error: no matching function for call to 'A::A()'
10:12: note: candidates are:
4:3: note: A::A(int)
A(int index) : i(index) {}
^
4:3: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(const A&)
class A {
^
1:7: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(A&&)
1:7: note: candidate expects 1 argument, 0 provided
20:29: error: use of deleted function 'C::C(int)'
D(int i) : A(i), B(i), C(i) {}
^
15:12: note: 'C::C(int)' is implicitly deleted because the default definition would be ill-formed:
using A::A;
^
15:12: error: no matching function for call to 'A::A()'
15:12: note: candidates are:
4:3: note: A::A(int)
A(int index) : i(index) {}
^
4:3: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(const A&)
class A {
^
1:7: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(A&&)
1:7: note: candidate expects 1 argument, 0 provided
我写的代码是否符合标准?这是实现我正在尝试的目标的最佳方法,即将构造函数参数向下传递到多继承树到实际保存数据的公共基础 class 吗?或者我可以以某种方式简化它或让它也与 gcc 一起工作吗?我是否正确地假设 class 通过多个父代间接继承虚拟基础 class 总是必须直接显式调用基础的构造函数?
这是 GCC 错误 58751。您的代码应该像在 Clang 中一样编译。 GCC 过去在使用虚拟继承继承构造函数时遇到过问题。
解决方法是手动编写转发构造函数。
class B : virtual public A {
public:
B(int i) : A(i) {}
};
class C : virtual public A {
public:
C(int i) : A(i) {}
};
考虑以下代码:
class A {
int i;
public:
A(int index) : i(index) {}
int get() { return i; }
};
class B : virtual public A {
public:
using A::A;
};
class C : virtual public A {
public:
using A::A;
};
class D : public B, public C {
public:
D(int i) : A(i), B(i), C(i) {}
};
int main() {
D d(1);
return 0;
}
虽然 clang 3.7 接受上述内容,但带有 -std=c++11
的 gcc 4.8 会抱怨此代码:
In constructor 'D::D(int)':
20:29: error: use of deleted function 'B::B(int)'
D(int i) : A(i), B(i), C(i) {}
^
10:12: note: 'B::B(int)' is implicitly deleted because the default definition would be ill-formed:
using A::A;
^
10:12: error: no matching function for call to 'A::A()'
10:12: note: candidates are:
4:3: note: A::A(int)
A(int index) : i(index) {}
^
4:3: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(const A&)
class A {
^
1:7: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(A&&)
1:7: note: candidate expects 1 argument, 0 provided
20:29: error: use of deleted function 'C::C(int)'
D(int i) : A(i), B(i), C(i) {}
^
15:12: note: 'C::C(int)' is implicitly deleted because the default definition would be ill-formed:
using A::A;
^
15:12: error: no matching function for call to 'A::A()'
15:12: note: candidates are:
4:3: note: A::A(int)
A(int index) : i(index) {}
^
4:3: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(const A&)
class A {
^
1:7: note: candidate expects 1 argument, 0 provided
1:7: note: constexpr A::A(A&&)
1:7: note: candidate expects 1 argument, 0 provided
我写的代码是否符合标准?这是实现我正在尝试的目标的最佳方法,即将构造函数参数向下传递到多继承树到实际保存数据的公共基础 class 吗?或者我可以以某种方式简化它或让它也与 gcc 一起工作吗?我是否正确地假设 class 通过多个父代间接继承虚拟基础 class 总是必须直接显式调用基础的构造函数?
这是 GCC 错误 58751。您的代码应该像在 Clang 中一样编译。 GCC 过去在使用虚拟继承继承构造函数时遇到过问题。
解决方法是手动编写转发构造函数。
class B : virtual public A {
public:
B(int i) : A(i) {}
};
class C : virtual public A {
public:
C(int i) : A(i) {}
};