关于虚拟基类和C ++中的虚拟继承 [英] about virtual base class and virtual inheritance in C++

查看:164
本文介绍了关于虚拟基类和C ++中的虚拟继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

gcc c ++虚拟继承问题


$ b我正在阅读有效的C ++由scott myers书。有关虚拟基类和虚拟继承的提到如下。


管理虚拟基类初始化的规则更复杂,比非虚拟碱基的那些。用于初始化虚拟基础的可重负性由层次结构中最多的派生类承载。当一个新的派生类添加到层次结构时,它必须承担其虚拟基础(直接和间接)的初始化职责。


在上面的语句中,什么是初始化虚拟基本calsses的规则和什么是派生类必须采取的责任,如上文中提到的。请求以示例解释。



谢谢!

解决方案

我以一个例子解释报价。假设你有这些类,

  struct A {A(int x){}}; 
struct B:virtual A {B(int x):A(x){}};
struct C:virtual A {C(int x):A(x){}};

注意: A / p>

现在您定义 D 源自 B C

  struct D:B,C 
{
//正确的构造函数 - 因为A(x)在初始化列表中存在
D(int x):A(x),B(x),C(x){}

//错误的构造函数 - 因为A(x)从初始化列表中不存在!
// D(int x):B(x),C(x){}

};

请注意 D > c> c> c> $ c $ c>,这就是为什么 D 在它的构造函数initialization-list中显式写入 A(x) )。如果您只写 B(x) C(x)甚至不编译。请参阅: http://www.ideone.com/sO6m5



但是一旦你写了 A(x),它编译得很好。请参阅: http://www.ideone.com/kiwh0



现在再次阅读报价,并注意粗体文本:


管理初始化的规则
的虚拟基类比
更复杂且不直观的是非虚拟基类。 用于初始化
虚拟基础的
responsobility由层次结构中最多的
派生类承载。

新派生类添加到
层次结构,它必须为其
虚拟库(直接和间接的
)承担
初始化职责



b $ b

我希望这个解释可以帮助你理解这个概念!


Possible Duplicate:
gcc c++ virtual inheritance problem

Hi All, I am reading Effective C++ by scott myers books. It was mentioned about virtual base class and virtual inheritance as follows.

The rules governing the initialization of virtual base classes are more complicated and less intuitive than are those for non-virtual bases. The responsobility for initializing a virtual base is borne by the most derived class in the hierarchy. When a new derived class is added to the hierarchy, it must assume initialization responsiblities for its virtual bases (both direct and indirect)

Question is in above statement what are the rules for initialization of virtual base calsses and what are the responsiblities that derivied class has to take as mentioned in above text. Kindly request to explain with example.

Thanks!

解决方案

Let me explain the quotation by an example. Suppose, you've these classes,

struct A {  A(int x) {} };
struct B : virtual A {  B(int x) : A(x) {} };
struct C : virtual A {  C(int x) : A(x) {} };

Note: A is virtual base!

Now you define D deriving from B and C :

struct D : B, C 
{
  //correct constructor - because A(x) is "present" in the initialization-list
  D(int x) : A(x), B(x), C(x) { }

  //wrong constructor - because A(x) is "absent" from the initialization-list!
  //D(int x) :B(x), C(x) { }

};

Please note that D is the most derived class in the hierarchy, so the responsibility of initializing A is with D, that is why D explicitly writes A(x) in it's constructor initialization-list (see above). If you write only B(x) and C(x), then it would NOT even compile. See this: http://www.ideone.com/sO6m5

But once you write A(x), it compiles fine. See this: http://www.ideone.com/kiwh0

Now read the quotation again, and pay attention to the bold text:

The rules governing the initialization of virtual base classes are more complicated and less intuitive than are those for non-virtual bases. The responsobility for initializing a virtual base is borne by the most derived class in the hierarchy. When a new derived class is added to the hierarchy, it must assume initialization responsiblities for its virtual bases (both direct and indirect)

I hope this explanation helps you understanding the concept!

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

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