[英]Boost smart pointers
什么时候应该使用intrusive_ptr而不是shared_ptr?
当你已经有一个存储在你指向的对象内的引用计数器时。
From Beyond the C++ Standard Library: An Introduction to Boost By Björn Karlsson 来自超越C ++标准库:Boost的简介作者:BjörnKarlsson
In most situations, you should not use boost::intrusive_ptr, because the functionality of shared ownership is readily available in boost::shared_ptr, and a non-intrusive smart pointer is more flexible than an intrusive smart pointer.
在大多数情况下,您不应该使用boost :: intrusive_ptr,因为共享所有权的功能在boost :: shared_ptr中很容易获得,而非侵入式智能指针比侵入式智能指针更灵活。 However, there are times when one needs an intrusive reference count, perhaps for legacy code or for integration with third-party classes.
但是,有时候需要一个侵入式引用计数,可能是遗留代码或与第三方类集成。 When the need arises, intrusive_ptr fits the bill, with the same semantics as the other Boost smart pointer classes.
当需要时,intrusive_ptr适合账单,具有与其他Boost智能指针类相同的语义。
By using another of the Boost smart pointers, you ensure a consistent interface for all smart pointer needs, be they intrusive or not. 通过使用另一个Boost智能指针,您可以确保所有智能指针需求的一致界面,无论它们是否具有侵入性。 The reference count must be provided by the classes that are used with intrusive_ptr.
引用计数必须由与intrusive_ptr一起使用的类提供。 intrusive_ptr manages the reference count by making unqualified calls to two functions, intrusive_ptr_add_ref and intrusive_ptr_release;
intrusive_ptr通过对两个函数intrusive_ptr_add_ref和intrusive_ptr_release进行非限定调用来管理引用计数; these functions must properly manipulate the intrusive reference count for intrusive_ptrs to work correctly.
这些函数必须正确操作侵入引用计数,以使intrusive_ptrs正常工作。 For all cases where a reference count already exists in the types that are to be used with intrusive_ptr, enabling support for intrusive_ ptr is as easy as implementing those two functions.
对于要与intrusive_ptr一起使用的类型中已存在引用计数的所有情况,启用对intrusive_ptr的支持就像实现这两个函数一样简单。
Use intrusive_ptr when 使用intrusive_ptr时
What Nick said. 尼克说的话。 One real-life example is managing COM pointers.
一个真实的例子是管理COM指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.