本文介绍了虚拟继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! virtual继承的意义是什么? 我看到下面的代码,不明白关键字 virtual 在以下上下文中: class A {}; B类:public virtual A; 解决方案虚拟继承用于解决DDD问题查看下面的示例,其中有两个类继承自同一个基类: class Base { public: virtual void Ambig }; C:public Base { public: // ... }; class D:public Base { public: // ... } 现在,您要创建一个新类继承了C和D类(它们都继承了Base :: Ambig()函数): class Wrong:public C,public D { public: ... }; 在上面定义Wrong类时,实际上创建了DDD(Diamond Derivation问题)因为你不能调用: 错误; wrong.Ambig(); 这是一个模糊的函数,因为它定义了两次: Wrong :: C :: Base :: Ambig() $ b b 和: Wrong :: D :: Base :: Ambig() 为了防止这种问题,你应该使用虚拟继承,这将知道引用正确的 Ambion() function。 所以 - define: class C:public virtual Base class D:public virtual Base class Right:public C,public D What is the meaning of "virtual" inheritance?I saw the following code, and didn't understand the meaning of the keyword virtual in the following context: class A {};class B : public virtual A; 解决方案 Virtual inheritance is used to solve the DDD problem (Dreadful Diamond on Derivation).Look at the following example, where you have two classes that inherit from the same base class:class Base{public: virtual void Ambig();};class C : public Base{public://...};class D : public Base{public: //...};Now, you want to create a new class that inherits both from C and D classes (which both have inherited the Base::Ambig() function):class Wrong : public C, public D{public:...};While you define the "Wrong" class above, you actually created the DDD (Diamond Derivation problem), because you can't call:Wrong wrong;wrong.Ambig();This is an ambiguous function because it's defined twice:Wrong::C::Base::Ambig()And:Wrong::D::Base::Ambig()In order to prevent this kind of problem, you should use the virtual inheritance, which will know to refer to the right Ambig() function.So - define:class C : public virtual Baseclass D : public virtual Baseclass Right : public C, public D 这篇关于虚拟继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 08:35