问题描述
EDIT3:请务必在回答前清楚地了解我的问题(有EDIT2和许多意见)。有很多答案清楚地表明对问题的误解(我知道这也是我的错,对不起)
Please be sure to clearly understand what I am asking before answering (there are EDIT2 and lots of comments around). There are (or were) many answers which clearly show misunderstanding of the question (I know that's also my fault, sorry for that)
您好,我看过在C ++中的虚拟继承问题( class B:public virtual A {...}
),但没有找到我的问题的答案。
Hi, I've looked over the questions on virtual inheritance (class B: public virtual A {...}
) in C++, but did not find an answer to my question.
我知道虚拟继承存在一些问题,但我想知道的是,在哪些情况下,虚拟继承会被认为是一个好的设计。
I know that there are some issues with virtual inheritance, but what I'd like to know is in which cases virtual inheritance would be considered a good design.
我看到人们提到了 IUnknown
或 ISerializable
iostream
设计是基于虚拟继承。这些是良好使用虚拟继承的好例子,是因为没有更好的选择,或者因为虚拟继承是这种情况下的正确设计吗?谢谢。
I saw people mentioning interfaces like IUnknown
or ISerializable
, and also that iostream
design is based on virtual inheritance. Would those be good examples of a good use of virtual inheritance, is that just because there is no better alternative, or because virtual inheritance is the proper design in this case? Thanks.
编辑:为了澄清,我在问现实生活中的例子,请不要给出抽象的例子。我知道虚拟继承是什么,哪些继承模式需要它,我想知道的是当它是做事情的好方法,而不仅仅是复杂继承的后果。
To clarify, I'm asking about real-life examples, please don't give abstract ones. I know what virtual inheritance is and which inheritance pattern requires it, what I want to know is when it is the good way to do things and not just a consequence of complex inheritance.
EDIT2:换句话说,我想知道何时钻石层次结构(这是虚拟继承的原因)是一个好的设计
In other words, I want to know when the diamond hierarchy (which is the reason for virtual inheritance) is a good design
推荐答案
如果你有一个接口层次结构和一个相应的实现层次结构,使接口基类虚拟库是必要的。
If you have an interface hierarchy and a corresponding implementation hierarchy, making the interface base classes virtual bases is necessary.
/ p>
E.g.
struct IBasicInterface
{
virtual ~IBasicInterface() {}
virtual void f() = 0;
};
struct IExtendedInterface : virtual IBasicInterface
{
virtual ~IExtendedInterface() {}
virtual void g() = 0;
};
// One possible implementation strategy
struct CBasicImpl : virtual IBasicInterface
{
virtual ~CBasicImpl() {}
virtual void f();
};
struct CExtendedImpl : virtual IExtendedInterface, CBasicImpl
{
virtual ~CExtendedImpl() {}
virtual void g();
};
通常这只有当你有一些接口扩展基本接口和多个实施战略需要在不同的情况下。这样,您具有清晰的接口层次结构,您的实现层次结构可以使用继承,以避免常见实现的重复。如果你使用Visual Studio,你会得到很多警告C4250。
Usually this only makes sense if you have a number of interfaces that extend the basic interface and more than one implementation strategy required in different situations. This way you have a clear interface hierarchy and your implementation hierarchies can use inheritance to avoid the duplication of common implementations. If you're using Visual Studio you get a lot of warning C4250, though.
为了防止意外切片,通常最好是 CBasicImpl
和类不是可实例化的,而是具有进一步的继承级别,不提供额外的功能保存构造函数。
To prevent accidental slicing it is usually best if the CBasicImpl
and CExtendedImpl
classes aren't instantiable but instead have a further level of inheritance providing no extra functionality save a constructor.
这篇关于当虚拟继承是一个好的设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!