fork download
  1. #include <memory>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. class AllocatedClass
  6. {
  7. public:
  8. AllocatedClass(int n = 0) : dummy(n) {}
  9. ~AllocatedClass() { std::cout << "I am being destroyed" << '\n'; }
  10. private:
  11. int dummy;
  12. };
  13.  
  14. class AllocatingClass
  15. {
  16. public:
  17. AllocatingClass() {}
  18. void AddNewObject(int num = 0)
  19. { shared_list.push_back(std::make_shared<AllocatedClass>(num)); }
  20. private:
  21. static std::vector<std::shared_ptr<AllocatedClass>> shared_list;
  22. };
  23.  
  24. std::vector<std::shared_ptr<AllocatedClass>> AllocatingClass::shared_list;
  25.  
  26. AllocatingClass ac;
  27.  
  28. int main()
  29. {
  30. ac.AddNewObject();
  31. ac.AddNewObject(1);
  32. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
I am being destroyed
I am being destroyed