C ++多继承问题 [英] C++ Multiple Inheritance Question

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

问题描述

产生这种情况的场景非常复杂,因此我将删除几个部分,并准确反映所涉及的类别。

The scenario generating this is quite complex so I'll strip out a few pieces and give an accurate representation of the classes involved.

/* This is inherited using SI by many classes, as normal */
class IBase
{
 virtual string toString()=0;
};

/* Base2 can't inherit IBase due to other methods on IBase which aren't appropriate */
class Base2
{
 string toString()
 {
  ...
 }
};

/* a special class, is this valid? */
class Class1 : public IBase, public Base2
{
};

那么,这是有效的吗? toString会有冲突吗?或者Class1可以使用Base2 :: toString来满足IBase?
像我说的有很多其他东西没有显示,所以建议主要设计更改这个例子可能不是有用的...虽然任何一般的建议/建议是欢迎的。

So, is this valid? Will there be conflicts on the toString? Or can Class1 use Base2::toString to satisfy IBase? Like I say there are lots of other things not shown, so suggestions for major design changes on this example are probably not that helpful... though any general suggestions/advice are welcome.

我的其他想法是这样的:

My other thought was something like this:

class Class1 : public IBase, public Base2
{
 virtual string toString()
 {
   return Base2::toString();
 }
};

这会建立和链接,但我不知道它是否有隐藏的错误。

This builds and links, but I've no idea if it has hidden bugs.

推荐答案

您可以使用虚拟继承修复此问题。

You can fix this using "virtual inheritance".

只有定义纯虚拟 toString 方法的基类(或者,在现实中,任何方法的纯虚拟版本都适用于 IBase Base2 ),例如

Consider creating a common base class that only defines a pure virtual toString method (or, in reality, pure virtual version of whatever methods it makes sense for both IBase and Base2 to have), e.g.

class Stringable {
  public:
    virtual string toString() = 0;
};

然后,让 IBase Base2 继承此 Stringable 类:

class IBase : public Stringable
{
};

class Base2 : public Stringable
{
public:
   virtual string toString()
   { ... }
};

现在这个还是不行,因为 Class1 将继承 Stringable 基类的两个副本,只有其中一个具有 toString的实现在其继承层次结构中。这被称为可怕的钻石 。但是,如果 IBase Base2 要从继承< Stringable ,像这样:

Now this as it is still won't work because Class1 will have inherited two copies of the Stringable base class, only one of which has an implementation for toString in its inheritance hierarchy. This is known as the "dreaded diamond". However, if IBase and Base2 were to inherit virtually from Stringable, like this:

class IBase : virtual public Stringable
{
};

class Base2 : virtual public Stringable
{
public:
   virtual string toString()
   { ... }
};

然后这告诉编译器应该只有一个常见的 Stringable 基类,即使 IBase Base2 都由一个类继承,因为那么 Base2 :: toString 用于 Class1

Then this tells the compiler that there should only be one common Stringable base class even if IBase and Base2 are both inherited by one class, which is exactly what you want, because then the Base2::toString is used for Class1.

你的第二个想法没有隐藏的bug,但是在屁股中有点痛苦,反复实现,因为你可能意识到。

Your second idea has no "hidden bugs", but is somewhat of a pain in the butt to implement repeatedly, as you probably realize.

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

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