fork(1) download
  1. #include <iostream>
  2.  
  3. struct Foo { virtual const char * who() = 0; };
  4. struct Bar : Foo { virtual const char * who() { return "Bar"; } };
  5. struct Baz : Foo { virtual const char * who() { return "Baz"; } };
  6. struct Qux : Bar, Baz { };
  7.  
  8. int main()
  9. {
  10. Qux * qux = new Qux();
  11. // Foo * foo = qux; // compile-time error, ambiguous
  12. Bar * bar = qux;
  13. Foo * foo = bar;
  14.  
  15. std::cout << static_cast<Baz *>(foo)->who() << " at "
  16. << static_cast<Baz *>(foo) << "\n";
  17. std::cout << dynamic_cast<Baz *>(foo)->who() << " at "
  18. << dynamic_cast<Baz *>(foo) << "\n";
  19.  
  20. delete qux;
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Bar at 0x8f7c008
Baz at 0x8f7c00c