Advertisement
kalabukdima

Untitled

Nov 12th, 2020 (edited)
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <functional>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <sstream>
  9.  
  10. struct Value {
  11.     virtual std::string ToString() const = 0;
  12.     virtual ~Value() {}
  13. };
  14.  
  15. struct Int : public Value {
  16.     Int(int value) : value(value) {}
  17.  
  18.     std::string ToString() const override {
  19.         return std::to_string(value);
  20.     }
  21.  
  22.     int value;
  23. };
  24.  
  25. struct List : public Value {
  26.     std::string ToString() const override {
  27.         std::stringstream ss;
  28.         ss << "[";
  29.         for (const auto& e : list) {
  30.             ss << e->ToString() << ", ";
  31.         }
  32.         ss << "]";
  33.         return ss.str();
  34.     }
  35.  
  36.     std::vector<std::unique_ptr<Value>> list;
  37. };
  38.  
  39. int main() {
  40.     auto l1 = std::make_unique<List>();
  41.     l1->list.emplace_back(std::make_unique<Int>(5));
  42.     auto l2 = std::make_unique<List>();
  43.     l2->list.emplace_back(std::make_unique<Int>(2));
  44.     l2->list.emplace_back(std::move(l1));
  45.     std::cout << l2->ToString() << "\n";
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement