gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:47:54 24 4
gpt4 key购买 nike

我想我在这里遗漏了一些简单的东西。我正在使用 Boost 的 shared_ptr

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

现在,我想切换 pB,使其包含 pA 的内容,减少 pB 的引用计数。我该怎么做?

最佳答案

这一切都是自动完成的:

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.

关于C++ shared_ptr - 附加到一个新的原始指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3305801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com