简体   繁体   中英

C++ shared_ptr - attach to a new raw pointer?

I think I'm missing something simple here. I'm using Boost's shared_ptr .

shared_ptr<Foo> pA(new Foo()); 
shared_ptr<Foo> pB(new Foo()); 

Now, I want to switch pB so it contains the contents of pA , decrementing the ref count of pB . How can I do this?

It's all done automatically:

pB = pA;  // pB ref count is decrement (in this case causing the value to be released)
          // pB is then made to point at the same value as pA
          // Thus incrementing the refCount.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM