我试图理解多重继承,这是我的代码:
struct A {
A() {}
static int n;
static int increment() { return ++n; }
};
int A::n = 0;
struct B : public A {};
struct C : public A {};
struct D : public B, C {};
int main() {
D d;
cout<<d.increment()<<endl;
cout<<d.increment()<<endl;
}
此代码有效。但是,如果我将 increment()
更改为非静态,它将失败。
我的问题:
- 为什么编译器提示非静态版本的
increment()
的调用不明确,而静态版本却满足? - 如果我向 B 或 C 添加另一个
increment()
函数,编译器也会报错,甚至声明为静态的。为什么?
最佳答案
模棱两可是什么意思?
编译器在给定上下文无法决定调用哪个函数时会提示调用不明确。因此,为了理解投诉,您必须检查可能存在的歧义。
Why compiler complains ambiguous call of non-static version of increment(), while satisfies with the static one?
根据定义,static
类的功能不依赖于类的任何实例。您可以将其称为 A::increment()
,这一事实强调了这一点。 (看,没有实例)。
菱形继承的问题不是编译器不知道执行哪个代码,而是不知道哪个this
提供(您的 A
对象中有两个 D
,一个包含在 B
中,一个包含在 C
中)。
当您使用 static
A
的功能, 没有隐式 this
已通过,所以没有问题;如果您尝试使用非 static
函数,则编译器无法决定是否 this
应该指向 A
在B
或在 C
, 这是模棱两可的。
If I add another increment() function to B or C, compiler will complain too, even declared as static. Why?
此时,编译器可能会在 B::increment()
之间进行选择和 C::increment()
,它应该选择哪个?这是模棱两可的。
当你有一个线性层次结构时,它会调用与其“最接近”的层次结构(将那些隐藏在继承树的下方),但这里 B
和 C
是两个独立的分支,没有“更好”的分支。
注意:即使 B
不执行 increment
, 自 A
你可以调用B::increment()
吗实际上调用A::increment()
. C
也是如此.
关于c++ - 没有虚拟继承的多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377040/