多重继承与冲突方法 [英] Multiple Inheritance with clashing methods

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

问题描述

我写了一些模板化的纯虚拟基类,它们是多重继承的,发现了一个小的奇怪的过程。它的关键是,如果你在两个基类中定义相同的方法,继承两个编译和工作正常,并且看起来你只需要一个单一的定义在派生类。我很好奇这里的幕后情况是什么,这是正确和计划的行为还是危险的编译器监督?



请参阅下面的说明性代码示例:

 命名空间
{
template< typename T_NumType>
class InheritFrom
{
public:
virtual void doSomething(const T_NumType& numType)= 0;

virtual void sharedMethod()= 0;

}; // class

class MultipleInheritor:public InheritFrom< int> public InheritFrom< float>
{
public:
void doSomething(const int& numType){}
void doSomething(const float& numType){}

void sharedMethod ){} // one definition here

}; // class

}

int main(int argc,char ** argv)
{
MultipleInheritor mult;
mult.doSomething(5);
mult.sharedMethod();

}

编辑:



从10.3:虚函数:



< blockquote>

在任何构造良好的类中,对于在
类或其直接或间接
基类中声明的每个
虚函数,都有唯一的最终
overrider,覆盖该函数
和该
函数的所有其他overrider。


意味着对于任何虚拟函数,将找到单个最终超越符。这通过10.2:成员名称查找中详细说明的规则完成。



因此,在我提供的情况下,确实有两个基类函数,并且由于成员名称查找,派生类中的单个函数被确定为 final overrider 。因此,我拥有的是完全良好的形式和C ++编译器工作的逻辑结果。

解决方案

函数有不同的签名(一个需要一个int,一个需要一个float),因此不是真的相同。



模板魔术模糊了一点,但它就像你有 foo(int) foo(float) - 这是两个不同的函数。你可以有 foo(int)const foo(int),那将是两个不同的函数。 / p>

编辑:好的,让我解决实际问题(如你的评论中所述)。它在技术上仍然不明确: InheritFrom 的每个版本都有自己的vtable,并且 MultipleInheritor 有一个vtable。您可以选择范围 sharedMethod (例如 InheritFrom< int> :: sharedMethod())的任何父实现,对于一个调用者,您的 MultipleInheritor 类型的对象有一个vtable,其中一个条目 sharedMethod p>

编辑编辑:关键是你在你的子类中实现 sharedMethod 。如果它不是纯的,并且在 MultipleInheritor 中没有实现,那么将会是一个编译器错误,因为它不清楚放入vtable中的一个插槽。


I am writing some templated pure virtual base classes that are multiply inherited and discovered a minor oddity in the process. The crux of it is, if you define the same method in two base classes, inheriting from both compiles and works fine and it appears that you only need a single definition in the derived class. I am curious as to what is going on behind the scenes here, is this correct and planned behaviour or a dangerous compiler oversight?

See below for illustrative code sample:

namespace
{
  template <typename T_NumType>
  class InheritFrom
  {
  public:
    virtual void doSomething(const T_NumType& numType) = 0;

    virtual void sharedMethod() = 0;

  }; // class

  class MultipleInheritor : public InheritFrom<int>, public InheritFrom<float>
  {
  public:
    void doSomething(const int& numType) {}
    void doSomething(const float& numType) {}

    void sharedMethod() {} // one definition here

  }; // class

}

int main(int argc, char** argv)
{
  MultipleInheritor mult;
  mult.doSomething(5);
  mult.sharedMethod();

}

EDIT:
The answers below and looking at the C++98 standard finally cleared this up for me.

From 10.3: Virtual Functions:

in any well-formed class, for each virtual function declared in that class or any of its direct or indirect base classes there is a unique final overrider that overrides that function and every other overrider of that function.

So this means that for any virtual function a single final overrider will be found. This is done through the rules detailed in 10.2: Member Name Lookup.

So in the case I have presented there are indeed two base class functions and due to member name lookup the single function in the derived class is determined as the final overrider for both. So what I have is perfectly well formed and a logical result of the workings of the C++ compiler.

解决方案

The functions have different signatures (one takes an int, and one takes a float), and as such are not really "the same".

The template magic obfuscates that a little bit, but it's the same as if you had foo(int) and foo(float) - those are two different functions. You could have foo(int) const and foo(int), those would be two different functions as well.

EDIT: Okay, let me address the actual question (as outlined in your comment). It's technically still not ambiguous: Each version of InheritFrom has its own vtable, and MultipleInheritor has one vtable. You can choose to scope any parent implementation of sharedMethod (like InheritFrom<int>::sharedMethod()), but as far as a caller is concerned, your object of type MultipleInheritor has one vtable with one entry for sharedMethod.

EDIT EDIT: The key is that you're implementing sharedMethod in your subclass. If it wasn't pure and there was no implementation in MultipleInheritor, there WOULD be a compiler error since it wouldn't be clear what to put into the one slot in the vtable.

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

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