intrusive_ptr:为什么不提供一个公共基类? [英] intrusive_ptr: Why isn't a common base class provided?

查看:282
本文介绍了intrusive_ptr:为什么不提供一个公共基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boost :: intrusive_ptr 需要 intrusive_ptr_add_ref intrusive_ptr_release 。为什么不提供一个基类来做这个?这里有一个示例: http://lists.boost.org/Archives /boost/2004/06/66957.php ,但海报说我不一定认为这是一个好主意。为什么不呢?

boost::intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release to be defined. Why isn't a base class provided which will do this? There is an example here: http://lists.boost.org/Archives/boost/2004/06/66957.php, but the poster says "I don't necessarily think this is a good idea". Why not?

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

Update: I don't think the fact that this class could be misused with Multiple Inheritance is reason enough. Any class which derives from multiple base classes with their own reference count would have the same issue. Whether these refcounts are implemented via a base class or not makes no difference.

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

I don't think there's any issue with multithreading; boost::shared_ptr offers atomic reference counting and this class could too.

推荐答案

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

Boost provides a facility for that. It can be configured for either thread-safe or thread-unsafe refcounting:

#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

这篇关于intrusive_ptr:为什么不提供一个公共基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆