多重继承的不确定基 [英] Ambiguous base with multiple inheritance

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

问题描述

我试图在一个大的库中写一些类的子类。我得到一个含糊的基地错误。这是一个可编译的问题的例子:

  #include< iostream> 

//我不能改变这些,因为它们在库中:
class InteractorStyle {};
类InteractorStyleCamera:public InteractorStyle {};
类InteractorStyleImage:public InteractorStyle {};

//这是我的子类(所以我可以改变它们):
class PointSelector:public InteractorStyle {};
class PointSelector2D:public InteractorStyleCamera,public PointSelector
{
//此函数必须完全如下所示(库的要求):
static PointSelector2D * SafeDownCast(InteractorStyle * o)
{
return static_cast< PointSelector2D *>(o);
}
};


int main()
{

return 0;
}

错误是



< blockquote>

错误:'InteractorStyle'是'PointSelector2D'的不明确的基础。


你的问题是Interactor风格被继承了两次 - 一次是PointSelector2D,一次是InteractorStyleCamera。



查看:



如何避免如果我使用多重继承,C ++中的死亡钻石?



尝试虚拟继承。


I am trying to write some subclasses of classes in a big library. I am getting an "ambiguous base" error. Here is a compilable example of the problem:

#include <iostream>

// I can't change these because they are in the library:
class InteractorStyle {};
class InteractorStyleCamera : public InteractorStyle {};
class InteractorStyleImage : public InteractorStyle {};

// These are my subclasses (so I can change them):
class PointSelector : public InteractorStyle {};
class PointSelector2D : public InteractorStyleCamera, public PointSelector
{
  // This function has to exist exactly like this (a requirement of the library):
  static PointSelector2D* SafeDownCast(InteractorStyle *o)
  {
    return static_cast<PointSelector2D *>(o);
  } 
};


int main()
{

  return 0;
}

The error is

error: ‘InteractorStyle’ is an ambiguous base of ‘PointSelector2D’.

Is there anything I can do in this case?

解决方案

Your problem is that Interactor style is inherited twice - once by PointSelector2D and once by InteractorStyleCamera. This means that you have 2 versions of every member of it contained within your class.

Check out:

How can avoid the Diamond of Death in C++ if I use multiple inheritance?

And try virtual inheritance.

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

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