方法冲突的多重继承

发布于 2024-10-30 08:52:25 字数 1146 浏览 1 评论 0原文

我正在编写一些模板化的纯虚拟基类,它们是多重继承的,并在这个过程中发现了一个小奇怪之处。其关键在于,如果您在两个基类中定义相同的方法,则从两个基类继承都可以编译并正常工作,并且似乎您只需要在派生类中进行单个定义。我很好奇这里的幕后发生了什么,这是正确且有计划的行为还是危险的编译器疏忽?

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

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();

}

编辑:
下面的答案和查看 C++98 标准终于为我解决了这个问题。

从 10.3 开始:虚拟函数:

在任何结构良好的类中,对于每个 其中声明的虚函数 类或其任何直接或间接 基础班有一个独特的期末考试 重写该函数的重写器 以及其他所有重写者 功能。

因此,这意味着对于任何虚拟函数,都会找到一个最终重写器。这是通过 10.2:成员名称查找中详述的规则来完成的。

因此,在我提出的情况下,确实有两个基类函数,并且由于成员名称查找,派生类中的单个函数被确定为两者的最终重写器。所以我所得到的是完全正确的,并且是 C++ 编译器工作的逻辑结果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2024-11-06 08:52:25

这些函数具有不同的签名(一个采用 int,一个采用 float),因此并不真正“相同”。

模板魔法稍微混淆了这一点,但它就像你有 foo(int) 和 foo(float) 一样 - 这是两个不同的函数。您可以使用 foo(int) constfoo(int),它们也将是两个不同的函数。

编辑:好的,让我解决实际问题(如您的评论中所述)。从技术上讲,这一点仍然不含糊:InheritFrom 的每个版本都有自己的 vtable,而 MultipleInheritor 有一个 vtable。您可以选择限制 sharedMethod 的任何父实现(例如 InheritFrom::sharedMethod()),但就调用者而言,您的类型对象MultipleInheritor 有一个 vtable,其中有一个 sharedMethod 条目。

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

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.

記柔刀 2024-11-06 08:52:25

用派生类中的一个函数定义覆盖两个同名的虚函数是 C++ 中多重继承的功能之一。

Bjarne Stroustrup 描述了一种使用虚拟函数实现 MI 的可能方法:多重继承对于 C++

基本上,MultipleInheritorInheritFrom vtable 和 InheritFrom vtable 都会被修改,以便 MultipleInheritor 的两个条目code>sharedMethod 指向 MultipleInheritor::sharedMethod

Overriding two virtual functions of the same name with one function definition in the derived class is one feature of multiple inheritance in C++.

Bjarne Stroustrup described one possible implementation of MI with virtual functions here: Multiple Inheritance for C++

Basically the InheritFrom<int> vtable and InheritFrom<float> vtable of MultipleInheritor get modified so that both entries for sharedMethod point to MultipleInheritor::sharedMethod.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文