Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://habr.com/article/353768/
- // problem #1
- #include <string>
- #include <memory>
- #include <iostream>
- #include <iomanip>
- class Item
- {
- public:
- virtual ~Item() { }
- Item(const char* data): m_data(std::make_unique<std::string>(data)) { }
- Item(Item&& obj): m_data(std::move(obj.m_data)) { }
- virtual const std::string GetContent() const { return m_data ? *m_data : ""; }
- private:
- std::unique_ptr<std::string> m_data;
- };
- class ItemEx : public Item
- {
- public:
- ItemEx(const char* data, const char* dataEx): Item(data), m_dataEx(std::make_unique<std::string>(dataEx)) { }
- ItemEx(ItemEx&& obj)
- : Item(std::move(obj))
- , m_dataEx(std::move(obj.m_dataEx))
- { }
- virtual const std::string GetContent() const override { return Item::GetContent() + (m_dataEx ? *m_dataEx : ""); }
- private:
- std::unique_ptr<std::string> m_dataEx;
- };
- void Func()
- {
- ItemEx item("123", "456");
- std::cout << item.GetContent() << std::endl;
- ItemEx newItem = std::move(item);
- std::cout << newItem.GetContent() << std::endl;
- std::cout << item.GetContent() << std::endl;
- }
- int main()
- {
- Func();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement