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)andfoo(float)– those are two different functions. You could havefoo(int) constandfoo(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
InheritFromhas its own vtable, andMultipleInheritorhas one vtable. You can choose to scope any parent implementation ofsharedMethod(likeInheritFrom<int>::sharedMethod()), but as far as a caller is concerned, your object of typeMultipleInheritorhas one vtable with one entry forsharedMethod.EDIT EDIT: The key is that you’re implementing
sharedMethodin your subclass. If it wasn’t pure and there was no implementation inMultipleInheritor, there WOULD be a compiler error since it wouldn’t be clear what to put into the one slot in the vtable.