#include #include #include #include #include using namespace std; class IntVal; class StringVal; class AbstractVal { public: virtual ~AbstractVal() {} virtual void print() = 0; virtual void copyFrom(AbstractVal& other) = 0; virtual void copyTo(IntVal& other) = 0; virtual void copyTo(StringVal& other) = 0; }; typedef unique_ptr valptr; class IntVal : public AbstractVal { public: int x_; IntVal(int x) : x_(x) {} void print() override { cout << x_; } void copyFrom(AbstractVal& other) override { other.copyTo(*this); } void copyTo(IntVal& other) override; void copyTo(StringVal& other) override; }; class StringVal : public AbstractVal { public: string x_; StringVal(string x) : x_(x) {} void print() override { cout << x_; } void copyFrom(AbstractVal& other) override { other.copyTo(*this); } void copyTo(IntVal& other) override; void copyTo(StringVal& other) override; }; // Definici metody IntVal::copyTo(StringVal&) bylo potreba presunout az za definici tridy StringVal, aby // kompilator uz vedel o datove polozce StringVal::x_ (definice ostatnich metod se pro prehlednost presunuly taky) void IntVal::copyTo(IntVal& other) { other.x_ = this->x_; } void IntVal::copyTo(StringVal& other) { other.x_ = to_string(this->x_); } void StringVal::copyTo(IntVal& other) { stringstream ss(this->x_); ss >> other.x_; } void StringVal::copyTo(StringVal& other) { other.x_ = this->x_; } int main() { valptr a(make_unique(123)); valptr b(make_unique("456")); valptr c(make_unique(789)); a->copyFrom(*b); a->print(); cout << endl; b->copyFrom(*c); b->print(); return 0; }