Boost logo

Boost Users :

From: Mark Storer (mstorer_at_[hidden])
Date: 2002-05-30 12:35:48


Is there an easier way to use shared_ptr as a lazy copy method?

It seems to me that the only way to do it would be by testing unique() (or
use_count()) every time the pointed-to object was to be changed:

boost::shared_ptr<foo> bar(new foo);
boost::shared_ptr<foo> baz = bar;

if(!baz->unique())
        ... // make a deep copy
baz->ChangeAFoo();

I can't think of a good way to alter shared_ptr either. The only thing I
came up with would be const and non-const versions of operator-> and
operator* where the non-const versions checked unique() and made a deep
copy. This would seem to fall into that last question in the FAQ anyway...
"Why doesn't shared_ptr provide (your pet feature here)". I'd think this
could easily lead to unnecessary deep copies unless the programmer took
explicit steps (lots of const_cast's) to avoid it.

Am I missing anything?

--Mark Storer
  Software Engineer
  Cardiff Software

#include <disclaimer>
typedef std::disclaimer<Cardiff> Discard;


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net