This code doesn't compile because of §10.3/2, i.e., virtual function A::f
has more than one final overrider in D
.
#include <iostream>
class A { public: virtual void f(){ std::cout << "A::f" << '\n'; } };
class B : public virtual A { public: void f(){ std::cout << "B::f" << '\n'; } };
class C : public virtual A { public: void f(){ std::cout << "C::f" << '\n'; } };
class D : public B, public C { };
int main()
{
D d;
d.f();
}
But contrary to my expectations this code compiles. Given that §10.3/2 contains this sentence, For convenience we say that any virtual function overrides itself
, it seems to me we have here the same problem mentioned above, i.e., the virtual function A::f
has more than one final overrider in D
, that is, A::f
and C::f
. As a matter of fact the call d.f()
invokes C::f
. Why is that?
#include <iostream>
class A { public: virtual void f(){ std::cout << "A::f" << '\n'; } };
class B : public virtual A {};
class C : public virtual A { public: void f(){ std::cout << "C::f" << '\n'; } };
class D : public B, public C { };
int main()
{
D d;
d.f();
}