fork download
  1. #include <iostream>
  2.  
  3. struct Base
  4. {
  5. virtual void f(){ std::cout << "Base::f()" << std::endl; }
  6. };
  7. struct Derived
  8. : Base
  9. {
  10. virtual void f() override { std::cout << "Derived::f()" << std::endl; }
  11. };
  12.  
  13. int main()
  14. {
  15. Derived d;
  16. Base &b = d;
  17.  
  18. d.f(); //calls Derived::f()
  19. b.f(); //calls Derived::f()
  20. d.Base::f(); //calls Base::f()
  21. b.Base::f(); //calls Base::f()
  22. }
  23.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
Derived::f()
Derived::f()
Base::f()
Base::f()