fork download
  1. #include <iostream>
  2.  
  3. struct Base
  4. {
  5. virtual void print(std::ostream& os) const { os << "Base"; }
  6. virtual ~Base() {}
  7. };
  8.  
  9. struct Derived1 : Base
  10. {
  11. void print(std::ostream& os) const { os << "Derived1"; }
  12. };
  13.  
  14. std::ostream & operator <<(std::ostream & os, const Base & x) {
  15. x.print(os);
  16. return os;
  17. }
  18.  
  19. #include <vector>
  20. int main()
  21. {
  22. Base b;
  23. Derived1 d;
  24. std::vector<const Base*> v{&b, &d};
  25.  
  26. for (auto it = v.cbegin(); it != v.cend(); ++it)
  27. {
  28. std::cout << *(*it) << std::endl;
  29. }
  30.  
  31. }
  32.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Base
Derived1