fork download
  1.  
  2. #include <string>
  3. #include <map>
  4. #include <memory>
  5. #include <iostream>
  6.  
  7. struct Base
  8. {
  9. virtual void print() = 0;
  10. };
  11.  
  12. struct Derived0 : Base
  13. {
  14. void print() override { std::cout << "d0" << std::endl; }
  15. };
  16.  
  17. struct Derived1 : Base
  18. {
  19. void print() override { std::cout << "d1" << std::endl; }
  20. };
  21.  
  22.  
  23. int main()
  24. {
  25.  
  26. std::map<std::string, std::unique_ptr<Base>> map;
  27.  
  28.  
  29. map["d0"] = std::make_unique<Derived0>();
  30. map["d1"] = std::make_unique<Derived1>();
  31.  
  32. for(auto& b : map)
  33. {
  34. b.second->print();
  35. }
  36. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
d0
d1