fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct A {
  6. virtual void foo () { cout<<"nothing!"<<endl; }
  7. };
  8. struct B : A {
  9. void foo () override { cout<<"this is a B object: foo!"<<endl; }
  10. };
  11. struct C : A {};
  12.  
  13. int main() {
  14. vector<A*> va;
  15. va.push_back (new A);
  16. va.push_back (new B);
  17. va.push_back(new C);
  18. for (auto x : va)
  19. x->foo();
  20. return 0;
  21. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
nothing!
this is a B object: foo!
nothing!