class "class" 不存在默认构造函数
No default constructor exists for class "class"
我有一组 diamond-shape 派生的 classes:
A
/ \
B C
\ /
D
class A { //this is an abstract class
protected:
const double commonVariable;
public:
A(const double& commonVariable) : commonVariable(commonVariable) {}
virtual double foo() const = 0; //pure virtual method
};
class B : virtual public A { //inherited virtually: no duplicates in D
protected:
const double& variableForB;
public:
//here I have to call the constructor A() since there's no default constructor
B(const double& commonVariable, const double& variableForB) : A(commonVariable), variableForB(variableForB) {}
double foo() const override { //this is just a common alias for the function in the instantiated class
bar();
}
double bar() const {
//do stuff
}
};
class C : virtual public A { //inherited virtually: no duplicates in D
protected:
const double& variableForC;
public:
C(const double& commonVariable, const double& variableForC) : A(commonVariable), variableForC(variableForC) {}
double foo() const override {
spam();
}
double spam() const {
//do stuff
}
};
class D : public B, public C {
public:
//even if I pass commonVariable twice no probs since A is virtually inherited
D(const double& commonVariable, const double& variableForB, const double& variableForC) : B(commonVariable, variableForB), C(commonVariable, varibaleForC) {} //<----THE ISSUE IS HERE (read below)
double foo() const override {
egg();
}
double egg() const {
do stuff
}
//some other methods
};
//somewhere else inside the program
D specificObject = D(commonVariable, variableForB, variableForC);
A* genericObject = &D;
genericObject->foo();
现在,除了一件事,一切正常。
在 class D 的构造函数中,我得到以下错误:
class "A"
不存在默认构造函数
以这种方式编写实际上是可行的:
D(const double& commonVariable, const double& variableForB, const double& variableForC) : A(commonVariable), B(commonVariable, variableForB), C(commonVariable, varibaleForC) {}
调用B的构造函数和C的构造函数时,不是应该自动调用A的构造函数吗?
我不明白为什么需要这种冗余...如果它是一个 10-class 树,我将不得不为每个派生调用 parents' 构造函数 10 次class.
无论如何,我对多态世界还很陌生,所以任何关于如何改进这些 classes 的建议都将不胜感激。
提前致谢
如果 A 没有删除其默认构造函数(通过定义 any 构造函数),此代码将起作用。要么你必须在 D.
的初始化列表中调用 A 构造函数
在虚拟继承的情况下,B 和 C 的实例是 class D 的子对象,但它们不包含 class A 的子对象。相反,A 的单个实例是 class D 的子对象D.
这样 D 中只有一个 A,这解决了所谓的诅咒钻石,代价是 B、C 和 D 不再具有标准内存布局(“memcpy-able”),即使它没有任何虚拟成员和 D 都应该有权访问 A 的构造函数以便能够对其进行初始化。
我有一组 diamond-shape 派生的 classes:
A
/ \
B C
\ /
D
class A { //this is an abstract class
protected:
const double commonVariable;
public:
A(const double& commonVariable) : commonVariable(commonVariable) {}
virtual double foo() const = 0; //pure virtual method
};
class B : virtual public A { //inherited virtually: no duplicates in D
protected:
const double& variableForB;
public:
//here I have to call the constructor A() since there's no default constructor
B(const double& commonVariable, const double& variableForB) : A(commonVariable), variableForB(variableForB) {}
double foo() const override { //this is just a common alias for the function in the instantiated class
bar();
}
double bar() const {
//do stuff
}
};
class C : virtual public A { //inherited virtually: no duplicates in D
protected:
const double& variableForC;
public:
C(const double& commonVariable, const double& variableForC) : A(commonVariable), variableForC(variableForC) {}
double foo() const override {
spam();
}
double spam() const {
//do stuff
}
};
class D : public B, public C {
public:
//even if I pass commonVariable twice no probs since A is virtually inherited
D(const double& commonVariable, const double& variableForB, const double& variableForC) : B(commonVariable, variableForB), C(commonVariable, varibaleForC) {} //<----THE ISSUE IS HERE (read below)
double foo() const override {
egg();
}
double egg() const {
do stuff
}
//some other methods
};
//somewhere else inside the program
D specificObject = D(commonVariable, variableForB, variableForC);
A* genericObject = &D;
genericObject->foo();
现在,除了一件事,一切正常。
在 class D 的构造函数中,我得到以下错误: class "A"
不存在默认构造函数以这种方式编写实际上是可行的: D(const double& commonVariable, const double& variableForB, const double& variableForC) : A(commonVariable), B(commonVariable, variableForB), C(commonVariable, varibaleForC) {}
调用B的构造函数和C的构造函数时,不是应该自动调用A的构造函数吗?
我不明白为什么需要这种冗余...如果它是一个 10-class 树,我将不得不为每个派生调用 parents' 构造函数 10 次class.
无论如何,我对多态世界还很陌生,所以任何关于如何改进这些 classes 的建议都将不胜感激。
提前致谢
如果 A 没有删除其默认构造函数(通过定义 any 构造函数),此代码将起作用。要么你必须在 D.
的初始化列表中调用 A 构造函数在虚拟继承的情况下,B 和 C 的实例是 class D 的子对象,但它们不包含 class A 的子对象。相反,A 的单个实例是 class D 的子对象D.
这样 D 中只有一个 A,这解决了所谓的诅咒钻石,代价是 B、C 和 D 不再具有标准内存布局(“memcpy-able”),即使它没有任何虚拟成员和 D 都应该有权访问 A 的构造函数以便能够对其进行初始化。