fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct AbstractShape
  7. {
  8. virtual std::ostream& dump(std::ostream&) = 0;
  9. };
  10. struct Point : AbstractShape
  11. {
  12. Point(float x, float y) : x(x), y(y) {}
  13.  
  14.  
  15. virtual std::ostream& dump(std::ostream& o) override
  16. {
  17. return o << "P[" << x << ":" << y << "]";
  18. }
  19. float x, y;
  20. };
  21. struct Line : AbstractShape
  22. {
  23. Line(float x1, float y1, float x2, float y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
  24.  
  25. virtual std::ostream& dump(std::ostream& o) override
  26. {
  27. return o << "L[" << x1 << ":" << y1 << "," << x2 << ":" << y2<< "]";
  28. }
  29.  
  30. float x1, y1, x2, y2;
  31. };
  32.  
  33. template<typename Object, typename Container, typename ...Args>
  34. Object* construct(Container& c, Args... args)
  35. {
  36. Object* res = new Object(args...);
  37. c.push_back(res);
  38. return res;
  39. }
  40. int main() {
  41. std::vector<AbstractShape*> container;
  42.  
  43. construct<Point>(container, 1, 2);
  44. construct<Line>(container, 1, 2, 3, 4);
  45.  
  46. for (auto s : container)
  47. s->dump(std::cout) << std::endl;
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
P[1:2]
L[1:2,3:4]