C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 多重继承模糊基类大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
代码
struct Base{};
struct Derived: public Base{};

struct A: public Base{};

struct B: public A,public Base{};

struct C: public A,public Derived{}; // why no ambiguity here?

int main() {}

编译器(g 5.1)警告

我明白这一点,基地在B.

>为什么C没有警告? C和C都不继承,它们都继承自Base?
>为什么添加虚拟

struct Derived: virtual Base{};

结果现在B和C发出警告,生活在Wandbox

解决方法

在B中,不可能直接引用继承的Base子对象的成员.虑:
struct Base {
    int x;
};

struct B: public A,public Base {
    void foo() {
        int& x1 = A::x; // OK
        int& x2 = x; // ambiguous
        // no way to refer to the x in the direct base
    }
};

在C这不是一个问题.可以使用限定名称来引用这两个x:

struct C: public A,public Derived {
    void foo() {
        int& x1 = A::x; // OK
        int& x2 = Derived::x; // OK
    }
};

所以你得到的警告是一个直接的基地也是通过另一个路径继承的唯一有用的.

对于你的第二个问题,我无法用g -5.1重现Coliru上的C的警告.

大佬总结

以上是大佬教程为你收集整理的c – 多重继承模糊基类全部内容,希望文章能够帮你解决c – 多重继承模糊基类所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。