9 Dec
2009
9 Dec
'09
4:26 p.m.
rv_nath wrote: > Dear Jeff, > Thanx for your quick reply. FUrther to the same question, > can you please explain: > > There is also weak_ptr originally intended to break cycles which may be > of use to you. > - the meaning of breaking cycles, and how it can help in this situation. > > It is said that a weak ptr doesn't increment the refcount. That means there > is a possibility of the pointee object being deleted, while a weak ptr is > holding a reference of it. So, in my understanding, if i return a weak-ptr > to the clients of my object, then I will be able to delete my object at time > of my choosing inspite of how many number of weak-ptr references the clients > may have. But after this, the weak-ptr reference becomes invalid. Am I > right? (please don't top post) Yes, that's right. See: http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/weak_ptr.htm You'd have to lock the weak_ptr to access a shared_ptr to the resource, which would litter your code with a lot of conditionals to check to see if the shared_ptr is not null. While this may be a valid design approach in some cases, my first inclination would be to redesign taking into account the true nature of the relationships your dealing with, IMHO of course. Jeff