问题:
class Base {
public:
Base(Base* pParent);
/* implements basic stuff */
};
class A : virtual public Base {
public:
A(A* pParent) : Base(pParent) {}
/* ... */
};
class B : virtual public Base {
public:
B(B* pParent) : Base(pParent) {}
/* ... */
};
class C : public A, public B {
public:
C(C* pParent) : A(pParent), B(pParent) {} // - Compilation error here
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
在给定的位置,gcc抱怨它无法匹配函数调用Base(),即默认构造函数.但是C不直接从Base继承,只通过A和B.那么为什么gcc会在这里抱怨?
想法?TIA/Rob
可能重复:
默认构造函数和虚拟继承
class Base
{
private:
int number;
protected:
Base(int n) : number(n) {}
public:
virtual void write() {cout << number;}
};
class Derived1 : virtual public Base
{
private:
int number;
protected:
Derived1(int n, int n2) : Base(n), number(n2) {}
public:
virtual void write() {Base::write(); cout << number;}
};
class Derived2 : virtual public Base
{
private:
int number;
protected:
Derived2(int n, int n2) : Base(n), number(n2) {}
public:
virtual void write() {Base::write(); cout << number;}
};
class Problematic …Run Code Online (Sandbox Code Playgroud)