简体   繁体   English

做boost :: shared_ptr <T> 和boost :: shared_ptr <const T> 分享参考计数?

[英]Do boost::shared_ptr<T> and boost::shared_ptr<const T> share the reference count?

There are several interesting questions on pitfalls with boost::shared_ptr s. 关于boost::shared_ptr的陷阱有几个有趣的问题。 In one of them, there is the useful tip to avoid pointing boost::shared_ptr<Base> and boost::shared_ptr<Derived> to the same object of type Derived since they use different reference counts and might destroy the object prematurely. 在它们中的一个,有有用的技巧,以避免指向boost::shared_ptr<Base>boost::shared_ptr<Derived>到类型的相同的对象Derived ,因为它们使用不同的引用计数,并且可能过早破坏的对象。

My question: Is it safe to have both boost::shared_ptr<T> and boost::shared_ptr<const T> point to the same object of type T , or will this cause the same problem? 我的问题:让boost::shared_ptr<T>boost::shared_ptr<const T>指向同一个T类型的对象是否安全,否则会导致同样的问题?

It is perfectly safe. 这是非常安全的。

The following code sample: 以下代码示例:

#include <iostream>
#include <boost/shared_ptr.hpp>

int main(int, char**)
{
  boost::shared_ptr<int> a(new int(5));
  boost::shared_ptr<const int> b = a;

  std::cout << "a: " << a.use_count() << std::endl;
  std::cout << "b: " << b.use_count() << std::endl;

  return EXIT_SUCCESS;
}

Compiles and run fine, and is perfectly correct. 编译并运行良好,完全正确。 It outputs: 它输出:

a: 2
b: 2

The two shared_ptr share the same reference counter. 两个shared_ptr共享相同的引用计数器。


Also: 也:

#include <iostream>
#include <boost/shared_ptr.hpp>

class A {};
class B : public A {};

int main(int, char**)
{
    boost::shared_ptr<A> a(new B());
    boost::shared_ptr<B> b = boost::static_pointer_cast<B>(a);

    std::cout << "a: " << a.use_count() << std::endl;
    std::cout << "b: " << b.use_count() << std::endl;

    return EXIT_SUCCESS;
}

Behave the same way. 表现方式相同。 You must, however never build your shared_ptr using a construct like this: 但您必须永远不会建立自己shared_ptr使用这样的结构:

boost::shared_ptr<A> a(new B());
boost::shared_ptr<B> b(static_cast<B*>(a.get()));

a.get() gives the raw pointer and loses all information about reference counting. a.get()给出了原始指针,并丢失了有关引用计数的所有信息。 Doing this, you'll end up with two distinct (not linked) shared_ptr that use the same pointer but different reference counters. 这样做,你最终会得到两个不同的(未链接的) shared_ptr ,它们使用相同的指针,但使用不同的引用计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从boost :: shared_ptr转换而来 <T> to boost :: shared_ptr <const T> - convert from boost::shared_ptr<T> to boost::shared_ptr<const T> 提高:: shared_ptr的 <const T> to boost :: shared_ptr <T> - boost::shared_ptr<const T> to boost::shared_ptr<T> 转换 const std::shared_ptr<const t> 进入 boost::shared_ptr<t></t></const> - convert const std::shared_ptr<const T> into boost::shared_ptr<T> 使用Boost shared_ptr进行嵌入式引用计数 - Embedded reference count with Boost shared_ptr 如何将boost :: atomic_store与shared_ptr一起使用 <T> 和shared_ptr <const T> ? - How to use boost::atomic_store with shared_ptr<T> and shared_ptr<const T>? 转换(隐式)提升shared_ptr <T> 到shared_ptr <const T> - Converting (implicit) boost shared_ptr<T> to shared_ptr<const T> 什么是boost的shared_ptr(shared_ptr <Y> const&r,T * p)用于? - What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for? const boost :: shared_ptr的目的 <T> &作为函数参数? - Purpose of const boost::shared_ptr<T>& as function argument? 使用Boost Python与shared_ptr <const T> - Using Boost Python with shared_ptr<const T> 一个神秘的编译错误:无法从&#39;const boost :: shared_ptr转换 <T> &#39;to&#39;const boost :: shared_ptr <T> “ - A mysterious compilation error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM