【问题标题】:C++ shared_ptr - attach to a new raw pointer?C++ shared_ptr - 附加到一个新的原始指针?
【发布时间】:2010-07-22 04:34:07
【问题描述】:

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

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

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

【问题讨论】:

  • “Boost 的std::tr1::shared_ptr”有点奇怪,你不觉得吗? :) Boost 有boost::shared_ptr,TR1 有std::tr1::shared_ptr,C++0x 有std::shared_ptr
  • Boost 还包含一个 TR1 库,其中包含 shared_ptr 它将 std::tr1::shared_ptr 解析为 c++ lib 的本机 std::shared_ptr,或者如果不存在则 boost::shared_ptr .所以 boost 的 std::tr1::shared_ptr 是有效且有意义的。

标签: c++ boost pointers shared-ptr


【解决方案1】:

这一切都是自动完成的:

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.

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多