
Hi Boosters
Let's say that I have many shared_ptr<A> sharing ownership to the same A object. At some point, I would like all those shared_ptr to point to a new Object A. Is there an easy (and safe) way to accomplish that? I know how to do that for one shared_ptr using either "reset", either operator = but in order to do this for all shared_ptr sharing ownership, the only way I could think of is through a registration mechanism (like the Observer pattern) but this sounds an overkilling process to me.
So basically, is there a "global" reset method? class A {
Matthieu Guillo 写道: public: explicit A(int v) : _v(v) { } int getV() { return _v; } private: int _v; }; shared_ptr< scoped_ptr<A> > sp_a( new scoped_ptr<A> ); (*sp_a).reset( new A(10) ); shared_ptr< scoped_ptr<A> > sp_b( sp_a ); shared_ptr< scoped_ptr<A> > sp_c( sp_a ); sp_a->reset( new A(20) ); assert( (*sp_b)->getV() == 20 ); assert( (*sp_c)->getV() == 20 );