You need to use shared_ptr instead of a raw pointer. In this case, your object will be deleted only when the last pointer to it dies.
Here is a small example of working with it :
#include <iostream> #include <vector> #include <memory> using namespace std; class Story { int n; public: Story(int n): n(n) { cout << "story #" << n << " created" << endl; } ~Story() { cout << "story #" << n << " destroyed" << endl; } }; int main() { vector<shared_ptr<Story>> list1, list2; cout << "creating #1 and adding to first list" << endl; list1.emplace_back(make_shared<Story>(1)); cout << "copying #1 to second list" << endl; list2.push_back(list1[0]); cout << "creating #2" << endl; auto story2 = make_shared<Story>(2); cout << "adding #2 to the second list" << endl; list2.push_back(story2); cout << "removing first ptr to story #2" << endl; story2 = nullptr; cout << "removing second ptr to story #2, now it will be destroyed" << endl; list2.resize(1); cout << "clearing first list" << endl; list1.clear(); cout << "clearing second list, now story #1 will be destroyed" << endl; list2.clear(); cout << "done" << endl; }
Conclusion:
creating # 1 and adding to first list
story # 1 created
copying # 1 to second list
creating # 2
story # 2 created
Adding # 2 to the second list
removing first ptr to story # 2
removing second ptr to story # 2, now it will be destroyed
story # 2 destroyed
clearing first list
clearing second list, now story # 1 will be destroyed
story # 1 destroyed
done
As @ixSci correctly suggests in the comments, you can delete the item completely in all lists if you accept a slightly different design. We divide lists into those owning their own elements (these lists will contain shared_ptr ), and non-owning (these lists will contain weak_ptr , non- weak_ptr pointer). Then when all strong links ( shared_ptr ) to an object die, weak weak_ptr ( weak_ptr ) will also become invalid.
This is made out as follows :
#include <iostream> #include <vector> #include <memory> #include <algorithm> using namespace std; class Story { int n; public: Story(int n): n(n) { cout << "story #" << n << " created" << endl; } ~Story() { cout << "story #" << n << " destroyed" << endl; } void print() { cout << "story #" << n << " reporting" << endl; } }; int main() { vector<shared_ptr<Story>> main_list; vector<weak_ptr<Story>> aux_list; cout << "creating and adding to owning list" << endl; main_list.emplace_back(make_shared<Story>(1)); main_list.emplace_back(make_shared<Story>(2)); cout << "copying to non-owning list" << endl; aux_list.push_back(main_list[0]); aux_list.push_back(main_list[1]); cout << "removing #2" << endl; main_list.resize(1); for (auto& weakptr : aux_list) { if (auto strongptr = weakptr.lock()) strongptr->print(); else cout << "(deleted entry)" << endl; } cout << "cleaning non-owning list" << endl; aux_list.erase( remove_if(begin(aux_list), end(aux_list), [](auto wp) { return wp.expired(); }), end(aux_list)); for (auto& weakptr : aux_list) { if (auto strongptr = weakptr.lock()) strongptr->print(); else cout << "(cannot happen)" << endl; } cout << "done" << endl; }
Conclusion:
creating and adding to owning list
story # 1 created
story # 2 created
copying to non-owning list
removing # 2
story # 2 destroyed
story # 1 reporting
(deleted entry)
cleaning non-owning list
story # 1 reporting
done
story # 1 destroyed
vector<shared_ptr<Story>> stories. (She has a <s> neon </ s> reference count.) - VladD