From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-07-13 13:21:08
--- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> > From: "Christian Thäter" <chth_at_g...>
> > > > shared_ptr<foo> p1(new foo());
> > > > shared_ptr<foo> p2(p1);
> > > > p1.reset(new foo());
> > > > assert(p1.get() == p2.get());
> > >
> > > Yes
> > What exactly is wrong with
> > *p1 = foo();
> Nothing, but this is not the same thing, so I'm not sure what you're
> driving at.
Pedantically speaking it's not the same thing. Consider the intent, however:
Christian wants p1.reset(new foo) to update all copies of p1 to point at the
new object; the old object is, obviously, destroyed, since no pointers to it
Now, assuming that 'foo' has a public assignment with reasonable semantics
and its identity is not important,
*p1 = foo();
accomplishes the intended effect. All copies of p1 now refer to the new
object, and the old object is lost.
In the case where 'foo' does not provide an assignment operator, it probably
does provide a 'reset', 'rebind', 'assign', or 'reopen' member function.
-- Peter Dimov Multi Media Ltd.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk