boost::intrusive_ptr
需要 intrusive_ptr_add_ref
和 intrusive_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/