开发者

Multiple Inheritance with clashing methods

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_NumT开发者_运维问答ype>
  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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜