fork download
  1. #include <iostream>
  2.  
  3. struct V {
  4. virtual void f(){std::cout << __PRETTY_FUNCTION__ << std::endl;}
  5. virtual void g(){std::cout << __PRETTY_FUNCTION__ << std::endl;}
  6. };
  7. struct A : virtual V {
  8. virtual void f(){std::cout << __PRETTY_FUNCTION__ << std::endl;}
  9. };
  10. struct B : virtual V {
  11. virtual void g(){std::cout << __PRETTY_FUNCTION__ << std::endl;}
  12. B(V*, A*);
  13. };
  14. struct D : A, B {
  15. virtual void f(){std::cout << __PRETTY_FUNCTION__ << std::endl;}
  16. virtual void g(){std::cout << __PRETTY_FUNCTION__ << std::endl;}
  17. D() : B((A*)this, this) { }
  18. };
  19.  
  20. B::B(V* v, A* a) {
  21. f(); // calls V::f, not A::f
  22. g(); // calls B::g, not D::g
  23. v->g(); // v is base of B, the call is well-defined, calls B::g
  24. a->f(); // undefined behavior, a’s type not a base of B
  25. }
  26.  
  27. int main(){
  28. D d;
  29. }
Success #stdin #stdout 0s 3348KB
stdin
Standard input is empty
stdout
virtual void V::f()
virtual void B::g()
virtual void B::g()
virtual void V::f()