while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon the following example:
#include<iostream>
using namespace std;
class Base {
public:
virtual void f(){
cout << "Base.f" << endl;
}
};
class Left : public Base { //NOT VIRTUAL!!!
public:
void g(){
f();
}
};
class Right : public Base{
public:
virtual void f(){
cout << "Right.f" << endl;
}
};
class Bottom : public Left, public Right{
public:
Bottom(int arg){ }
//void f() { }
};
int main(int argc,char **argv)
{
Bottom* b = new Bottom(23);
b->g();
}
It is clear that calling
b->f()
is ambiguous, so there is no unique method f()
on object Bottom. Now, calling
b->g()
works fine and prints
Base.f
Well, as far as I see this:
- static type is Bottom, so we call its
g()
method, as it is non-virtual g()
method is inherited from Left, so we call this inherited method- Now,
g()
in Left tries to call virtual methodf()
. According to C++ sepcification, we callf()
method of a dynamic type of our pointer (which is Bottom)
BUT Bottom does not have method f()
... at least not a unique one. Why does this program executes Left::Base::f()
rather than Right::Base::f()
or why does it simply not states that call to f()
is ambiguous from Bottom?