fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct A {
  5. virtual void foo() const {
  6. cout << "Hello from A" << endl;
  7. }
  8. };
  9.  
  10. struct B: A {
  11. virtual void foo() {
  12. cout << "Hello from B" << endl;
  13. }
  14. };
  15.  
  16. struct C: B {
  17. void foo() {
  18. cout << "Hello from C" << endl;
  19. }
  20. void foo() const {
  21. cout << "Hello from C const" << endl;
  22. }
  23. };
  24.  
  25. int main() {
  26. A* b = new B();
  27. b->foo();
  28. ((B*)b)->foo();
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Hello from A
Hello from B