Would it be smart to have a vector in an object with a list of pointers that point to it?
This way when the object is deleted, it could delete all the pointers pointing to it to prevent a null-pointer exception?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your ultimate goal is to detect when an object is freed from users of the object the best bet is to use a weak pointer semantic. There an implementation in the Boost Smart Pointer library (see weak_ptr). It solves the problem you describe by reversing the reference graph you describe. It has the pointers reference a single common counter instance which when the originator deletes the object it sets to zero. Hence because all the weak_ptr references point to the single counter they all see the change immediately and hence you can tell when the object goes away.