简体   繁体   English

用于将weak_ptr应用于unique_ptr的内容

[英]Use for applying weak_ptr to contents of unique_ptr

I'm trying to understand the unique_ptr , shared_ptr , and weak_ptr that came in with c++11. 我正在尝试理解c ++ 11中带来的unique_ptrshared_ptrweak_ptr

I've heard that weak_ptr 's would be nice for things like caching, and breaking cycles, and so on. 我听说weak_ptr对于缓存,打破周期等等都很好。 I've heard that they work well with shared_ptrs . 我听说他们与shared_ptrs配合得很好。

But in this regard, what's the difference between shared_ptrs and unique_ptrs ? 但在这方面, shared_ptrsunique_ptrs之间的区别是什么? Why does weak_ptr only get to be used with one and not the other? 为什么weak_ptr只能与一个而不是另一个一起使用? Why wouldn't I want to have a weak reference to something owned by someone else? 为什么我不想对其他人拥有的东西进行弱引用?

A weak_ptr is technically a means to hang on to the reference counter of a set of shared_ptr s that manage some shared object. 从技术上讲, weak_ptr是一种挂起管理某个共享对象的一组shared_ptr的引用计数器的方法。 When the last shared_ptr is destroyed the object is destroyed, but its reference counter lives on as long as there are weak_ptr s to it. 当最后一个shared_ptr被销毁时,对象被销毁,但只要有weak_ptr ,它的引用计数器就会存在。 Thus via any still exising weak_ptr you can check whether the object still exists, or has been destroyed. 因此,通过任何仍然存在的weak_ptr您可以检查对象是否仍然存在,或者是否已被销毁。

If it still exists then from the weak_ptr you can obtain a shared_ptr that lets you reference the object. 如果它仍然存在,那么从weak_ptr可以获得一个允许您引用该对象的shared_ptr

The main usage of this is to break cycles. 这主要用于打破周期。

In particular, an object can contain a weak_ptr holding on to its own reference counter, which allows you to obtain a shared_ptr to the object from the object itself. 特别是,一个对象可以包含一个持有其自己的引用计数器的weak_ptr ,它允许您从对象本身获取对象的shared_ptr That is, a shared_ptr that uses the same reference counter as other shared_ptr s to this object. 也就是说, shared_ptr使用与该对象的其他shared_ptr相同的引用计数器。 Which is how enable_shared_from_this works. 这就是enable_shared_from_this工作原理。

unique_ptr doesn't have any reference counter, so it doesn't make sense to hang on to that non-existent reference counter. unique_ptr没有任何引用计数器,因此挂起那个不存在的引用计数器是没有意义的。

The major point of a weak pointer is that you can try to make the pointer strong, that is owning: 弱指针的主要观点是你可以尝试使指针变强,即拥有:

auto strongPtr = weakPtr.lock();

if (strongPtr)
{
    // still existed, now have another reference to the resource
}
else
{
    // didn't still exist
}

Note the first path: making a weak pointer stronger requires we take ownership of the object . 注意第一条路径: 强弱指针要求我们取得对象的所有权

This is why it doesn't make sense with unique_ptr : the only way to make the weak pointer strong is to take the resource from somewhere else, and for unique_ptr that would mean leaving that somewhere else with an unexpected null pointer. 这就是使用unique_ptr没有意义的原因:使弱指针变强的唯一方法是从其他地方获取资源,而对于unique_ptr意味着将其留在其他地方并带有意外的空指针。 shared_ptr gets a pass because taking it really means sharing it. shared_ptr得到一个传递,因为它实际上意味着共享它。

I'm new to C++11 as well, so if someone knows better I would appreciate any corrections. 我也是C ++ 11的新手,所以如果有人知道的话,我会很感激任何更正。

I think that there wouldn't be much of a reason to, otherwise, you'd use a shared_ptr since it would defeat the whole purpose of a unique_ptr . 我认为没有太多理由,否则,你会使用shared_ptr因为它会unique_ptr的整个目的。 A unique_ptr has implied semantics that it has complete control over the object to which it points. unique_ptr隐含了语义,它可以完全控制它指向的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM