通过引用传递scoped_ptr(从类内的一个方法到另一个方法)是一种好的做法吗?
或者如果我需要这样做,那么我应该使用shared_ptr?
Or if i need to do that, then i should just use shared_ptr?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
或者如果我需要这样做,那么我应该使用shared_ptr?
Or if i need to do that, then i should just use shared_ptr?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
如果被调用者不需要存储包装的指针,而只是使用它来调用某些方法,则通过引用传递
scoped_ptr
是安全的。由scoped_ptr
保护的对象在超出范围时将被销毁 - 如果指针是堆栈变量,则在调用函数的末尾,或者在包含类实例被释放时(如果它)是一个成员变量。一般来说,智能指针用于管理对象所有权,因此这里有一个快速运行:
boost::scoped_ptr
将受保护的对象生命周期限制在封闭范围内,只有一个所有者。boost::shared_ptr
通过引用计数支持共享所有权,仅当引用计数为零时,受保护的对象才会被销毁。这是最通用的智能指针,但也是最昂贵的,因为它会产生一些较小的开销(引用计数是通过原子操作维护的,这相当昂贵。)还存在循环依赖的可能性。boost::weak_ptr
是一个非拥有智能指针,可以在运行时升级为boost::shared_ptr
,并检查受保护的指针对象还活着。还有像
boost::shared_array
这样的数组变体,因为 C++ 对于单个和多个对象有单独的释放函数(operator delete
与operator delete[]
.)智能指针支持资源获取就是初始化,或者RAII,习惯用法,这是一种方式提供例外安全保证。
It is safe to pass
scoped_ptr
by reference if the callee doesn't need to store the wrapped pointer, and just uses it to invoke some methods. The object guarded by thescoped_ptr
will be destroyed when it goes out of scope - either at the end of the calling function if the pointer was a stack variable, or when the containing class instance is deallocated, if it was a member variable.In general, smart pointers are there for managing object ownership, so here's a quick run down:
boost::scoped_ptr
restricts guarded object lifetime to the enclosing scope, there's only one owner.std::auto_ptr
there's also only one owner at a time, but it allows passing the ownership via assignment (as function parameter or return value.)boost::shared_ptr
supports shared ownership via reference counting, the guarded object is only destroyed when reference count goes to zero. This is the most versatile smart pointer, but also the most expensive since it suffers some minor overhead (the reference count is maintained with atomic operations, which are rather expensive.) There's also possibility of circular dependencies.boost::weak_ptr
is a non-owning smart pointer, which could be upgraded toboost::shared_ptr
at runtime with a check that the guarded object is still alive.There are also array variants like
boost::shared_array
since C++ has separate deallocation functions for single and multiple objects (operator delete
vs.operator delete[]
.)Smart pointers support the Resource Acquisition Is Initialization, or RAII, idiom, which is a way to provide exception safety guarantees.
是的,您可以通过引用传递它。
但是,如果函数只想使用托管对象,您可能会考虑传递对该对象本身的引用。
不同之处在于,在第一种情况下,您已将函数与特定的智能指针类型结合起来。
一般来说,如果目的是对
scoped_ptr
实例本身执行某些操作(例如释放或重置资源),我只会传递scoped_ptr
。类似地,对于shared_ptr
(例如,该函数想要与其他共享指针共享资源)。Yes you can pass it by reference.
However, if the function just wants to use the managed object, you might consider passing a reference to the object itself.
The difference is that in the first case you have coupled the function with a particular smart pointer type.
Generally I'd only pass the
scoped_ptr
, if the intention is to do something with thescoped_ptr
instance itself (e.g release or reset the resource). Similarly forshared_ptr
(e.g the function wants to share the resource with other shared pointers).就我个人而言,我几乎在所有地方都通过 const 引用传递shared_ptr。正如其他人所说,如果您调用函数并传递 const 引用,那么调用者几乎肯定会将 shared_ptr 保留在范围内。
真正的好处是您可以节省以这种方式更新参考计数器的成本。快速阅读一下 wiki http://en.wikipedia.org/wiki/Reference_counting 即可您将了解到,在引用计数器上不断执行 +1 / -1(大概是原子)操作会破坏您的缓存。
Personally I pass shared_ptr by const reference almost everywhere. As others have said, if you're calling a function and passing a const reference, then the caller almost certainly is going to keep the shared_ptr in scope.
The real benefit is that you save the cost of updating the reference counter this way. A quick read of the wiki http://en.wikipedia.org/wiki/Reference_counting and you'll learn that constantly performing +1 / -1 (presumably atomic) operations on the reference counter can ravage your cache.