fork download
  1. #include <list>
  2. #include <memory>
  3. #include <iostream>
  4.  
  5. struct Base
  6. {
  7. virtual void foo() = 0;
  8. };
  9.  
  10. struct Derived1 : Base
  11. {
  12. void foo() { std::cout << "Lol!\n"; }
  13. };
  14.  
  15. struct Derived2 : Base
  16. {
  17. void foo() { std::cout << "Haha!\n"; }
  18. };
  19.  
  20. int main()
  21. {
  22. typedef std::list<std::unique_ptr<Base>> list_type;
  23.  
  24. list_type l;
  25. l.push_back(list_type::value_type(new Derived1()));
  26. l.push_back(list_type::value_type(new Derived2()));
  27. l.push_back(list_type::value_type(new Derived2()));
  28. l.push_back(list_type::value_type(new Derived1()));
  29.  
  30. for (auto& x : l)
  31. x->foo();
  32. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Lol!
Haha!
Haha!
Lol!