#include #include #include #include using namespace std; class AbstractVal; typedef unique_ptr valptr; class AbstractVal { public: virtual ~AbstractVal() {} virtual void print() = 0; virtual valptr clone() = 0; }; class IntVal : public AbstractVal { public: IntVal(int x) : x_(x) {} void print() override { cout << x_; } valptr clone() override { return make_unique(*this); } private: int x_; }; class StringVal : public AbstractVal { public: StringVal(string x) : x_(x) {} void print() override { cout << x_; } valptr clone() override { return make_unique(*this); } private: string x_; }; class Seznam { public: Seznam() {} Seznam(const Seznam& s) { clone(s); } Seznam& operator=(const Seznam& s) { if (this != &s) { pole.clear(); clone(s); } return *this; } void add(valptr p) { pole.push_back(move(p)); } void print() { for (auto&& x : pole) { x->print(); } } private: vector pole; void clone(const Seznam& s) { for (auto&& x : s.pole) { pole.push_back(x->clone()); } } }; int main() { Seznam s1, s2; s1.add(make_unique(123)); s1.add(make_unique("456")); s2 = s1; s2.print(); s2 = s2; s2.print(); }