fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct base {
  7. virtual void foo() {
  8. cout << "base" << endl;
  9. }
  10. };
  11.  
  12. struct a: base {
  13. void foo() override {
  14. cout << "a" << endl;
  15. }
  16. };
  17. struct b: base {
  18. void foo() override {
  19. cout << "b" << endl;
  20. }
  21. };
  22.  
  23. int main() {
  24. vector<unique_ptr<base>> container;
  25.  
  26. for(int i = 0; i < 5; ++i) {
  27. container.emplace_back(new a{});
  28. container.emplace_back(new b{});
  29. }
  30.  
  31. for(auto &el: container) {
  32. el->foo();
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
a
b
a
b
a
b
a
b
a
b