c++ - 侵入式_ptr : Why isn't a common base class provided?

标签 c++ boost shared-ptr smart-pointers boost-smart-ptr

boost::intrusive_ptr需要 intrusive_ptr_add_refintrusive_ptr_release被定义为。为什么不提供一个可以做到这一点的基类?这里有一个例子:http://lists.boost.org/Archives/boost/2004/06/66957.php ,但海报说“我不一定认为这是个好主意”。为什么不呢?

更新:我认为这个类可能被多重继承滥用这一事实是不够的。任何从具有自己的引用计数的多个基类派生的类都会有同样的问题。这些引用计数是否通过基类实现都没有区别。

我认为多线程没有任何问题; boost::shared_ptr提供原子引用计数,这个类也可以。

最佳答案

Boost 为此提供了一个工具。它可以配置为线程安全或线程不安全的引用计数:

#include <boost/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>

class CMyClass
    : public boost::intrusive_ref_counter<
                               CMyClass,
                               boost::thread_unsafe_counter>
     ...

boost::intrusive_ptr<CMyClass> myPtr;

http://www.boost.org/doc/libs/1_62_0/libs/smart_ptr/intrusive_ref_counter.html

关于c++ - 侵入式_ptr : Why isn't a common base class provided?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903179/

相关文章:

c++ - firebreath 中的 IPC (boost c++)

c++ - 非常适合 Xeon-phi 众核架构的应用程序

c++ - 在 cmake 中设置最低版本的 boost

c++ - 试图编译 boost 依赖库 - "is private"错误

c++ - 在 (unordered_)set 中修改 shared_ptr 是否安全?

c++ - 如何初始化作为构造函数中的类成员的共享指针

c++ - 将客户数据存储到 C++ 中的二进制文件中

c++ - 如何使用 boost::asio 在 http 上进行 POST?

c++ - 从 unique_ptr<T[]> 初始化 shared_ptr<T>

c++ - 可以具有不同基成员实现的类的最佳实现