fork 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<shared_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. shared_ptr<Test> add(const string& S) {
  28. auto p = make_shared<Test>(S);
  29. _list.push_back(p);
  30. return p;
  31. }
  32. };
  33.  
  34. int main() {
  35. auto x = make_shared<Test>("one");
  36. x->add("two");
  37. auto y = x->add("three");
  38. y->add("four");
  39.  
  40. cout << y->to_string() << '\n';
  41. cout << x->to_string() << '\n';
  42. }
  43.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
[three,[four]]
[one,[two],[three,[four]]]