fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class Test {
  8. private:
  9. string _name;
  10. vector<unique_ptr<Test>> _list;
  11. public:
  12. explicit Test(const string& S) : _name(S) { }
  13.  
  14. string to_string() const {
  15. string s("[");
  16. s += _name;
  17.  
  18. for (auto const& p : _list) {
  19. s += ",";
  20. s += p->to_string();
  21. }
  22.  
  23. s += "]";
  24. return s;
  25. }
  26.  
  27. Test& add(const string& S) {
  28. _list.push_back(make_unique<Test>(S));
  29. return *(_list.back());
  30. }
  31. };
  32.  
  33. int main() {
  34. Test x("one");
  35. x.add("two");
  36. Test& y = x.add("three");
  37. y.add("four");
  38.  
  39. cout << y.to_string() << '\n';
  40. cout << x.to_string() << '\n';
  41. }
  42.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
[three,[four]]
[one,[two],[three,[four]]]