fork download
  1. #include <vector>
  2. #include <memory>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. struct Myobject : std::enable_shared_from_this< Myobject >
  7. {
  8. int nb;
  9. std::string name;
  10. std::shared_ptr<Myobject> next;
  11. Myobject(int nb, std::string name) { this->name=name; this->nb=nb; }
  12. ~Myobject() { std::cout << "deleting " << nb << " " << name << std::endl; }
  13. };
  14.  
  15. int main() {
  16. std::vector< std::shared_ptr< Myobject > > arr;
  17. arr.push_back(std::make_shared< Myobject >(1,"first"));
  18. arr.push_back(std::make_shared< Myobject >(2,"second"));
  19.  
  20. for (auto obj: arr) {
  21. std::cout << obj->nb << " " << obj->name << std::endl;
  22. }
  23.  
  24. arr[0]->next = arr[1];
  25. };
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
1 first
2 second
deleting 1 first
deleting 2 second