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