C++ shared_ptr - 附加到新的原始指针?

Nic*_*ner 2 c++ boost pointers shared-ptr

我想我在这里缺少一些简单的东西.我正在使用Boost的shared_ptr.

shared_ptr<Foo> pA(new Foo()); 
shared_ptr<Foo> pB(new Foo()); 
Run Code Online (Sandbox Code Playgroud)

现在,我想切换pB所以它包含的内容pA,减少引用的计数pB.我怎样才能做到这一点?

Mar*_*ork 9

这一切都是自动完成的:

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.
Run Code Online (Sandbox Code Playgroud)