没有虚拟继承的多重继承 [英] multiple inheritance without virtual inheritance

查看:106
本文介绍了没有虚拟继承的多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解多重继承,这是我的代码:

I am trying to understand multiple inheritance, here is my code:

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()更改为非静态,它将失败.

This code works. However, if I change increment() to non-static it will fail.

我的问题:

  1. 为什么编译器抱怨对increment()的非静态版本的模棱两可的调用,而又对静态版本感到满意?
  2. 如果我向B或C添加另一个increment()函数,编译器也会抱怨,甚至声明为静态.为什么?
  1. Why compiler complains ambiguous call of non-static version of increment(), while satisfies with the static one?
  2. If I add another increment() function to B or C, compiler will complain too, even declared as static. Why?

推荐答案

模棱两可是什么意思?

当编译器无法确定在给定上下文的情况下要调用哪个函数时,它将抱怨模棱两可的调用.因此,为了理解投诉,您必须检查可能存在的歧义.

A compiler complains of ambiguous calls when it cannot decide which function to call given the context. So, in order to understand the complaints, you have to check what the possible ambiguities could be.

为什么编译器抱怨增量()的非静态版本的模棱两可的调用,而又满足于静态版本呢?

Why compiler complains ambiguous call of non-static version of increment(), while satisfies with the static one?

根据定义,类的static函数不依赖于该类的任何实例.您可以称其为A::increment()(请参见无实例).

By definition, a static function of a class does not depend on any instance of the class. This is emphasized by the fact that you could call it A::increment() (see, no instance).

钻石继承的问题不是编译器不知道要执行哪个代码,而是不知道要提供哪个this(您的计算机中有两个A D对象,其中一个包含在B中,另一个包含在C中.)

The problem of the diamond inheritance is not that the compiler does not know which code to execute, it's that is does not know which this to provide (there are two A in your D object, one contained in B and one in C).

当使用Astatic函数时,不会传递任何隐式this,因此没有问题.如果尝试使用非static函数,则编译器无法确定this应该指向B还是C中的A,这是不明确的.

When you use a static function of A, no implicit this is passed, so there is no issue; if you try to use a non-static function, then the compiler cannot decide whether this should point to the A in B or in C, it's ambiguous.

如果我在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()之间进行选择,应该选择哪一个?模棱两可.

At this point, the compiler may choose between B::increment() and C::increment(), which should it pick? It's ambiguous.

当您具有线性层次结构时,它会调用它的最接近的"层次结构(它将那些层次结构隐藏在继承树的更下方),但是这里的BC是两个独立的分支,没有更好的"分支

When you have a linear hierarchy, it calls the "closest" to it (which hides those further down the inheritance tree), but here B and C are two independent branches and there is no "better" branch.

注意:即使B没有实现increment,由于A可以调用B::increment(),而实际上调用了A::increment().对于C.

Note: even if B does not implement increment, since A does you can call B::increment() which actually calls A::increment(). The same goes for C.

这篇关于没有虚拟继承的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆