shared_ptr的VS的scoped_ptr
的scoped_ptr 不能够复制和被删除了范围。因此,它是一种限制的shared_ptr 。因此,除了在案件似乎当你真的需要限制复制操作的shared_ptr 是更好地使用。因为有时候你不知道你需要创建对象或没有的副本。所以,问题是:除了上面提到的情况下,我们可以认为的shared_ptr 是更好的(或推荐)改为使用的scoped_ptr $ C的$ C>。请问的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.