fork download
  1. #include <vector>
  2. #include <memory>
  3. #include <iostream>
  4.  
  5. class parent{
  6. public:
  7. virtual int foo() = 0;
  8. };
  9. class child1:public parent{
  10. virtual int foo(){ return 1; }
  11. };
  12. class child2:public parent{
  13. virtual int foo(){ return 2; }
  14. };
  15.  
  16. int main(){
  17. std::vector<std::shared_ptr<parent>> v;
  18. v.push_back( std::shared_ptr<parent>(new child1()));
  19. v.push_back( std::shared_ptr<parent>(new child2()));
  20.  
  21. std::cout << "v[0]:" << v[0]->foo() << std::endl;
  22. std::cout << "v[1]:" << v[1]->foo() << std::endl;
  23. }
  24.  
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
v[0]:1
v[1]:2