shared_ptr的VS的scoped_ptr [英] shared_ptr vs scoped_ptr

查看:166
本文介绍了shared_ptr的VS的scoped_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的scoped_ptr 不能够复制和被删除了范围。因此,它是一种限制的shared_ptr 。因此,除了在案件似乎当你真的需要限制复制操作的shared_ptr 是更好地使用。因为有时候你不知道你需要创建对象或没有的副本。所以,问题是:除了上面提到的情况下,我们可以认为的shared_ptr 是更好的(或推荐)改为使用的scoped_ptr 。请问的scoped_ptr 工作快得多从的shared_ptr ,或者它有什么优势呢?

scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr. So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know you need to create a copy of your object or no. So the question is: besides the cases mentioned above, could we consider that shared_ptr is better (or recommended) to use instead of scoped_ptr. Does scoped_ptr work much faster from shared_ptr, or does it have any advantages?

谢谢!

推荐答案

的shared_ptr 的scoped_ptr 更重量级的。它需要分配和释放引用计数对象以及被管理的对象,并处理线程安全的引用计数 - 在一个平台上我的工作,这是一个显著的开销

shared_ptr is more heavyweight than scoped_ptr. It needs to allocate and free a reference count object as well as the managed object, and to handle thread-safe reference counting - on one platform I worked on, this was a significant overhead.

我的建议(一般)是使用符合自己需求的最简单的对象。如果您需要引用计数的共享,使用的shared_ptr ;如果你只需要自动删除,一旦你有一个参考结束后,使用的scoped_ptr

My advice (in general) is to use the simplest object that meets your needs. If you need reference-counted sharing, use shared_ptr; if you just need automatic deletion once you've finished with a single reference, use scoped_ptr.

这篇关于shared_ptr的VS的scoped_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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