在C ++中的虚拟继承 [英] Virtual inheritance in C++

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

问题描述

在c ++中阅读关于虚拟继承的网站时,我发现了这一点



当使用多重继承时,有时需要使用虚拟继承。一个很好的例子是标准的iostream类层次结构:

  //注意:这是iostream类的简化描述

class ostream:virtual public ios {/*..*/}
class istream:virtual public ios {/*..*/}

class iostream:public istream,public ostream {/*..*/}
//单个ios继承

C ++如何确保只有一个虚拟成员的实例存在,而不管派生自它的类的数量是多少? C ++使用附加级别的间接访问虚拟类,通常通过指针。换句话说,iostream层次结构中的每个对象都有一个指向ios对象的共享实例的指针。额外的间接级别具有轻微的性能开销,但它是一个小的支付价格。



我困惑的语句:



C ++使用额外级别的间接访问虚拟类,通常通过指针



基本上,如果不使用虚拟继承,基本成员实际上是派生类实例的一部分。基本成员的内存在每个实例中分配,并且不需要进一步的间接访问它们:

  class Base {
public:
int base_member;
};

类派生:public Base {
public:
int derived_member;
};


Derived * d = new Derived();
int foo = d-> derived_member; //只需要一个间接。
int bar = d-> base_member; // 同样在这里。
delete d;但是,当虚拟继承发挥作用时,虚拟基础成员被它们的继承树中的所有类共享。 ,而不是当基类是乘法继承时创建的多个副本。在您的示例中, iostream 只包含一个 ios 成员的共享副本,即使它们继承两次 istream ostream

  class Base {
public:
//由来自Intermediate1和Intermediate2的Derived共享。
int base_member;
};

class Intermediate1:​​virtual public Base {
};

class Intermediate2:virtual public Base {
};

类派生:public Intermediate1,public Intermediate2 {
public:
int derived_member;
};

这意味着需要额外的间接步骤才能访问虚拟基础成员:

  Derived * d = new Derived(); 
int foo = d-> derived_member; //只需要一个间接。
int bar = d-> base_member; //大致相当于
// d-> shared_Base-> base_member。
delete d;


I found this in a website while reading about virtual inheritance in c++

When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy:

//Note: this is a simplified description of iostream classes

class  ostream: virtual public ios { /*..*/ }
class  istream: virtual public ios { /*..*/ }

class iostream : public istream, public ostream { /*..*/ } 
//a single ios inherited

How does C++ ensure that only a single instance of a virtual member exists, regardless of the number of classes derived from it? C++ uses an additional level of indirection to access a virtual class, usually by means of a pointer. In other words, each object in the iostream hierarchy has a pointer to the shared instance of the ios object. The additional level of indirection has a slight performance overhead, but it's a small price to pay.

i am confused with the statement:

C++ uses an additional level of indirection to access a virtual class, usually by means of a pointer

could anybody explain this?

解决方案

Basically, if virtual inheritance is not used, base members are actually part of derived class instances. Memory for the base members is allocated in each instance, and no further indirection is necessary to access them:

class Base {
public:
    int base_member;
};

class Derived: public Base {
public:
    int derived_member;
};


Derived *d = new Derived();
int foo = d->derived_member;  // Only one indirection necessary.
int bar = d->base_member;     // Same here.
delete d;

However, when virtual inheritance comes into play, virtual base members are shared by all classes in their inheritance tree, instead of several copies being created when the base class is multiply inherited. In your example, iostream only contains one shared copy of the ios members, even though it inherits them twice from both istream and ostream.

class Base {
public:
    // Shared by Derived from Intermediate1 and Intermediate2.
    int base_member;  
};

class Intermediate1 : virtual public Base {
};

class Intermediate2 : virtual public Base {
};

class Derived: public Intermediate1, public Intermediate2 {
public:
    int derived_member;
};

That means an extra indirection step is required in order to access virtual base members:

Derived *d = new Derived();
int foo = d->derived_member;  // Only one indirection necessary.
int bar = d->base_member;     // Roughly equivalent to
                              // d->shared_Base->base_member.
delete d;

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

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