Hello, When I reset the main pointer to an object, I want all other pointers pointing to that object to be reset as well. Which type of smart pointer should I use? Example: boost::shared_ptr<MySillyClass> mySharedPtr1(new MySillyClass("test")); boost::shared_ptr<MySillyClass> mySharedPtr2 = mySharedPtr1; mySharedPtr1.reset(); if(mySharedPtr2.use_count() > 0) { TRACE("still used\n"); } else { TRACE("not used anymore\n"); } I am getting message "still used". I understand that is the normal behavior of boost::shared_ptr. Is there a smart pointer type that would print the second message? Reason: When I am done with an object, I would reset the main pointer to it and do not want other references to that object to be able to do anything with it. Thanks for advice! Vaclav
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Vaclav Sent: Monday, March 19, 2007 5:42 AM To: boost-users@lists.boost.org Subject: [Boost-users] which type of smart pointer to use? When I am done with an object, I would reset the main pointer to it and do not want other references to that object to be able to do anything with it. [Nat] Would it work for you to use (e.g.) shared_ptr for the main pointer and weak_ptr for every other reference?
That seems to work. Thank you! btw is this the correct test? if(myweakPtr2.use_count() > 0) or it there a better way to check whether a smart pointer is still pointing? Thanks Vaclav
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Vaclav Sent: 19 March 2007 14:19 To: boost-users@lists.boost.org Subject: Re: [Boost-users] which type of smart pointer to use?
That seems to work. Thank you!
btw is this the correct test?
if(myweakPtr2.use_count() > 0)
I believe you should lock the weak_ptr to a smart point and test that. shared_ptr<x> p = weak.lock() if (p) { ... } But I could be wrong! James
or it there a better way to check whether a smart pointer is still pointing?
Thanks Vaclav
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.
participants (3)
-
Hughes, James
-
Nat Goodspeed
-
Vaclav