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的警告. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 采用React+Ant Design组件化开发前端界面(一)
- SQLite 入门教程(三)好多约束 Constraints
- c# – Dropdown SelectedValue Set Problem
- swift – CKFetchRecordsOperation永远不会返回任何键
- c# – 从另一个文件加载部分App.Config
- 异步处理XML异步数据——以原生的JavaScript与jQuery中的$.
- 23种设计模式之策略模式
- c – Cygwin GDB在尝试启动程序时给出错误193
- 深入浅出JSONP--解决ajax跨域问题
- c# – 使用IDbConnection.Query()返回IEnumerable,AsList()