虚拟继承混乱 [英] Virtual Inheritance Confusion

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

问题描述

我正在阅读关于继承,我有一个主要的问题,我已经无法解决几个小时:

I'm reading about inheritance and I have a major issue that I haven't been able to solve for hours:

给定一个类 Bar 是具有 virtual 函数的类,

Given a class Bar is a class with virtual functions,

class Bar
{
    virtual void Cook();
};

有什么不同:

class Foo : public Bar
{
    virtual void Cook();
};

class Foo : public virtual Bar
{
    virtual void Cook();
};

?小时的谷歌和阅读提出了很多关于其用途的信息,但没有实际告诉我两者之间的区别,只是混淆我更多。

? Hours of Googling and reading came up with lots of information about its uses, but none actually tell me what the difference between the two are, and just confuse me more.

推荐答案

只有当类继承自
Foo 时,虚拟继承才有意义。如果我定义如下:

Virtual inheritance is only relevant if classes are to inherit from Foo. If I define the following:

class B {};
class L : virtual public B {};
class R : virtual public B {};
class D : public L, public R {};

然后最终对象将只包含一个 B ,由
L R 共享。没有 virtual ,类型 D 的对象将包含
两个副本 B ,一个在 L ,一个在 R

Then the final object will only contain one copy of B, shared by both L and R. Without the virtual, an object of type D would contain two copies of B, one in L, and one in R.

有一些参数,所有的继承都应该是虚拟的(因为
在它有区别,这是你想要的最多的
时间)。然而,在实践中,虚拟继承是昂贵的,并且在大多数情况下
是不必要的:在设计良好的系统中,大多数
继承将​​简单地是从一个或更多继承的具体类interfaces;这样一个具体的类通常不是设计为从
派生自己,所以没有问题。但有一些重要的
异常:例如,如果你定义一个接口,然后
扩展到接口,扩展应该继承基本接口
,因为一个具体的实现可以想要
实现几个扩展。或者如果你正在设计mixins,其中
某些类只实现接口的一部分,最后
类继承自其中的几个类(
接口的一部分)。最后,关于是否继承实际上
的标准不是太难:

There is some argument that all inheritance should be virtual (because in the cases where it makes a difference, that is what you want most of the time). In practice, however, virtual inheritance is expensive, and in most cases, not necessary: in a well designed system, most inheritance will simply be of a concrete class inheriting from one or more "interfaces"; such a concrete class is usually not designed to be derived from itself, so there is no problem. But there are important exceptions: if, for example, you define an interface, and then extensions to the interface, the extensions should inherit virtually from the base interface, since a concrete implementation could want to implement several extensions. Or if you are designing mixins, where certain classes only implement part of the interface, and the final class inherits from several of these classes (one per part of the interface). In the end, the criteron as to whether to inherit virtually or not isn't too difficult:


  • if继承不是公共的,它可能不应该是虚拟
    (我从来没有见过异常),否则

  • if the inheritance isn't public, it probably shouldn't be virtual (I've never seen an exception), otherwise

不设计为基类,不需要
虚拟继承,否则

if the class is not designed to be a base class, there's no need for virtual inheritance, otherwise

继承应该是虚拟的。

有一些例外,但上述规则违反了
安全;它通常是正确的,即使在
虚拟继承是不必要的情况下继承。

There are a few exceptions, but the above rules err on the side of safety; it's usually "correct" to inherit virtually even in cases where the virtual inheritance isn't necessary.

最后一点:虚拟基本必须初始化由最多的
派生类,直接继承的类(并声明
继承是virtual)。然而,在实践中,这是一个非问题。
如果你看看虚拟继承有意义的情况,它是
总是从接口继承的情况,它将不包含
数据,因此有(只)一个默认构造函数。如果你发现自己
从具有构造函数的类中继承了
参数,那么是时候问一些关于设计的严重问题。

One final point: a virtual base must always be initialized by the most derived class, not the class that directly inherits (and declares that the inheritance is virtual). In practice, however, this is a non-issue. If you look at the cases where virtual inheritance makes sense, it is always a case of inheriting from an interface, which will contain no data, and thus have (only) a default constructor. If you find yourself inheriting virtually from classes with constructors which take arguments, it's time to ask some serious questions about the design.

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

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