智能指针(升压)解释 [英] smart pointers (boost) explained

查看:110
本文介绍了智能指针(升压)解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是下面的一组指针的区别?当你用每个指针在生产中code,如果在所有?

What is the difference between the following set of pointers? When do you use each pointer in production code, if at all?

例子有AP preciated!

Examples would be appreciated!


  1. 的scoped_ptr

的shared_ptr

的weak_ptr

intrusive_ptr

你在生产中code使用boost?

Do you use boost in production code?

推荐答案

这很容易当你有,你可以为每个智能指针属性。有三个重要特性。

Basic properties of smart pointers

It's easy when you have properties that you can assign each smart pointer. There are three important properties.


  • 是在完全没有所有权

  • 所有权转让

  • 的所有权份额

  • no ownership at all
  • transfer of ownership
  • share of ownership

首先意味着智能指针不能删除对象,因为它不拥有它。第二意味着只有一个智能指针能永远指向同一对象在同一时间。如果智能指针是从函数返回,所有权转移至返回的智能指针,例如。

The first means that a smart pointer cannot delete the object, because it doesn't own it. The second means that only one smart pointer can ever point to the same object at the same time. If the smart pointer is to be returned from functions, the ownership is transferred to the returned smart pointer, for example.

第三意味着多个智能指针可以同时指向相同的对象。这适用于的原始指针的太多,但是原始指针缺少一个重要的特点:它们不定义它们是否的拥有的或没有。如果每一位业主放弃的对象所有权的智能指针的份额将删除对象。这种行为恰好是经常需要,因此,拥有共享智能指针广泛US $ p $垫。

The third means that multiple smart pointers can point to the same object at the same time. This applies to a raw pointer too, however raw pointers lack an important feature: They do not define whether they are owning or not. A share of ownership smart pointer will delete the object if every owner gives up the object. This behavior happens to be needed often, so shared owning smart pointers are widely spread.

一些拥有智能指针支持,无论是第二还是第三。 ,因此不能从函数返回或通过其他地方。这是最适合于智能指针保持本地和刚刚创建的,因此超出范围后,它释放的对象 RAII 的目的。

Some owning smart pointers support neither the second nor the third. They can therefore not be returned from functions or passed somewhere else. Which is most suitable for RAII purposes where the smart pointer is kept local and is just created so it frees an object after it goes out of scope.

所有权分享可以通过具有拷贝构造函数来实现。这自然复制智能指针和两个副本和原件将引用同一个对象。所有权的转移不能真正在C ++实现目前,因为没有办法从一个物体转移一些到其他的语言支持:如果您尝试从一个函数返回一个对象,正在发生的事情是,对象被复制。因此,实现所有权转移一个智能指针必须使用拷贝构造函数来实现所有权转移的。然而,这又打破了其在容器中使用,因为要求规定的容器元素的拷贝构造函数是与这些智能指针的这种所谓的移动构造的行为不兼容的某种行为。

Share of ownership can be implemented by having a copy constructor. This naturally copies a smart pointer and both the copy and the original will reference the same object. Transfer of ownership cannot really be implemented in C++ currently, because there are no means to transfer something from one object to another supported by the language: If you try to return an object from a function, what is happening is that the object is copied. So a smart pointer that implements transfer of ownership has to use the copy constructor to implement that transfer of ownership. However, this in turn breaks its usage in containers, because requirements state a certain behavior of the copy constructor of elements of containers which is incompatible with this so-called "moving constructor" behavior of these smart pointers.

C ++ 1X为转移的所有权通过引入所谓的移动构造函数和移动赋值运算符原生支持。它还配备了一个名为的unique_ptr 这种转移的拥有智能指针。

C++1x provides native support for transfer-of-ownership by introducing so-called "move constructors" and "move assignment operators". It also comes with such a transfer-of-ownership smart pointer called unique_ptr.

的scoped_ptr 是一个智能指针,既不转让,也不共享。这只是有用的,如果你需要在本地分配内存,但可以肯定它再次释放时,它超出范围。但它仍然可以与其他的scoped_ptr互换,如果你想这样做。

scoped_ptr is a smart pointer that is neither transferable nor sharable. It's just usable if you locally need to allocate memory, but be sure it's freed again when it goes out of scope. But it can still be swapped with another scoped_ptr, if you wish to do so.

的shared_ptr 是一个智能指针共享所有权(第三种以上)。它是引用计数,因此可以看到,当它的最后一个副本超出范围,然后将其释放所管理的对象。

shared_ptr is a smart pointer that shares ownership (third kind above). It is reference counted so it can see when the last copy of it goes out of scope and then it frees the object managed.

的weak_ptr 是一个非拥有智能指针。它被用于引用一个管理对象(由一个shared_ptr管理),不添加引用计数。通常情况下,你需要得到原始指针出的shared_ptr并复制左右。但是,这不会是安全的,因为你不会有一个方法来检查时,对象竟是删除。因此,weak_ptr的提供通过引用通过的shared_ptr管理的对象手段。如果您需要访问该对象,您可以锁定它的管理(以避免在另一个线程,而你使用对象一个shared_ptr释放它),然后使用它。如果weak_ptr的指向一个对象已被删除,它会抛出异常注意到你。引用计数不能轻易这样的情况应对:使用weak_ptr的,当你有一个循环引用是最有利的。

weak_ptr is a non-owning smart pointer. It is used to reference a managed object (managed by a shared_ptr) without adding a reference count. Normally, you would need to get the raw pointer out of the shared_ptr and copy that around. But that would not be safe, as you would not have a way to check when the object was actually deleted. So, weak_ptr provides means by referencing an object managed by shared_ptr. If you need to access the object, you can lock the management of it (to avoid that in another thread a shared_ptr frees it while you use the object) and then use it. If the weak_ptr points to an object already deleted, it will notice you by throwing an exception. Using weak_ptr is most beneficial when you have a cyclic reference: Reference counting cannot easily cope with such a situation.

intrusive_ptr 就像是一个shared_ptr,但它不保留引用计数在一个shared_ptr,但叶递增/递减计数需要被定义一些辅助功能被管理的对象。这具有的优点在于一个已经引用的对象(其具有由外部参考计数机制递增引用计数)可以被塞到一个intrusive_ptr - 因为引用计数是不再内部的智能指针,但智能指针使用现有引用计数机制。

intrusive_ptr is like a shared_ptr but it does not keep the reference count in a shared_ptr but leaves incrementing/decrementing the count to some helper functions that need to be defined by the object that is managed. This has the advantage that an already referenced object (which has a reference count incremented by an external reference counting mechanism) can be stuffed into an intrusive_ptr - because the reference count is not anymore internal to the smart pointer, but the smart pointer uses an existing reference counting mechanism.

的unique_ptr 是所有权指针的转移。您不能复制它,但你可以通过使用C ++ 1X的举动构造移动:

unique_ptr is a transfer of ownership pointer. You cannot copy it, but you can move it by using C++1x's move constructors:

unique_ptr<type> p(new type);
unique_ptr<type> q(p); // not legal!
unique_ptr<type> r(move(p)); // legal. p is now empty, but r owns the object
unique_ptr<type> s(function_returning_a_unique_ptr()); // legal!

这是语义的性病:: auto_ptr的服从,但由于缺少移动原生支持,所以未能提供他们没有缺陷。的unique_ptr会自动从临时其它的unique_ptr这是移动语义的主要特点之一窃取资源。 auto_ptr的将是赞成的unique_ptr在接下来的C ++标准发布pcated德$ P $。 C ++ 1X还将允许那些只有移动,但不可拷贝到容器馅的对象。所以,你可以东西的unique_ptr的成例如向量。我会在这里停下,并引用您<一个href=\"http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx\">a这个美好的文章如果您想了解更多关于这一点。

This is the semantic that std::auto_ptr obeys, but because of missing native support for moving, it fails to provide them without pitfalls. unique_ptr will automatically steal resources from a temporary other unique_ptr which is one of the key features of move semantics. auto_ptr will be deprecated in the next C++ Standard release in favor of unique_ptr. C++1x will also allow stuffing objects that are only movable but not copyable into containers. So you can stuff unique_ptr's into a vector for example. I'll stop here and reference you to a fine article about this if you want to read more about this.

这篇关于智能指针(升压)解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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