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

岁酱吖の 提交于 2019-12-11 02:25:37

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/3305801/c-shared-ptr-attach-to-a-new-raw-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!