unique_ptr和shared_ptr之间的差异 [英] Differences between unique_ptr and shared_ptr

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

问题描述


可能的重复项:

pimpl:shared_ptr或unique_ptr

智能指针(boost)解释

有人能解释shared_ptr和unique_ptr之间的区别吗? / p>

Could someone explain differences between shared_ptr and unique_ptr?

推荐答案

这两个类都是智能指针,这意味着它们会在大多数情况下自动释放它们指向的对象当该对象不能再被引用时。

Both of these classes are smart pointers, which means that they automatically (in most cases) will deallocate the object that they point at when that object can no longer be referenced. The difference between the two is how many different pointers of each type can refer to a resource.

当使用 unique_ptr 时,每个类型的指针可以指向一个资源。 ,最多只能有一个 unique_ptr 指向任何一个资源。当 unique_ptr 被销毁时,资源会自动回收。因为对任何资源只能有一个 unique_ptr ,任何尝试复制 unique_ptr 将导致编译 - 时间错误。例如,此代码是非法的:

When using unique_ptr, there can be at most one unique_ptr pointing at any one resource. When that unique_ptr is destroyed, the resource is automatically reclaimed. Because there can only be one unique_ptr to any resource, any attempt to make a copy of a unique_ptr will cause a compile-time error. For example, this code is illegal:

unique_ptr<T> myPtr(new T);       // Okay
unique_ptr<T> myOtherPtr = myPtr; // Error: Can't copy unique_ptr

但是, unique_ptr 可以使用新的移动语义移动

However, unique_ptr can be moved using the new move semantics:

unique_ptr<T> myPtr(new T);                  // Okay
unique_ptr<T> myOtherPtr = std::move(myPtr); // Okay, resource now stored in myOtherPtr

同样,你可以这样做:

unique_ptr<T> MyFunction() {
    unique_ptr<T> myPtr(/* ... */);

    /* ... */

    return myPtr;
}

这个成语的意思是我要返回一个托管资源给你,如果你不显式地捕获返回值,那么资源将被清除,如果你这样做,那么你现在拥有该资源的独占所有权。这样,您可以将 unique_ptr 视为更安全,更好地替代 auto_ptr

This idiom means "I'm returning a managed resource to you. If you don't explicitly capture the return value, then the resource will be cleaned up. If you do, then you now have exclusive ownership of that resource." In this way, you can think of unique_ptr as a safer, better replacement for auto_ptr.

shared_ptr 允许多个指针指向给定资源。当一个资源的最后一个 shared_ptr 被销毁时,资源将被释放。例如,此代码完全合法:

shared_ptr, on the other hand, allows for multiple pointers to point at a given resource. When the very last shared_ptr to a resource is destroyed, the resource will be deallocated. For example, this code is perfectly legal:

shared_ptr<T> myPtr(new T);       // Okay
shared_ptr<T> myOtherPtr = myPtr; // Sure!  Now have two pointers to the resource.

在内部, shared_ptr 使用参考计数以跟踪有多少指针指向资源,因此您需要小心不要引入任何参考周期。

Internally, shared_ptr uses reference counting to track how many pointers refer to a resource, so you need to be careful not to introduce any reference cycles.

总之:


  1. 使用 unique_ptr

  2. 当您需要多个时,使用 shared_ptr 指向相同资源的指针。

  1. Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed.
  2. Use shared_ptr when you want multiple pointers to the same resource.

希望这有助于!

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

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