智能指针:或谁拥有你的宝贝? [英] Smart Pointers: Or who owns you baby?

查看:108
本文介绍了智能指针:或谁拥有你的宝贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++与内存拥有权有关

Aka所有权语义

C++ is all about memory ownership
Aka "Ownership Semantics"

一个动态分配的内存块的所有者释放该内存。因此,问题真正成为谁拥有内存。

It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory.

在C ++中,所有权归类为一个RAW指针被包装在一个好的(IMO)C ++程序中非常罕见的[RARE不是从来没有]看到RAW指针传递(作为RAW指针没有推断所有权,因此我们不能告诉谁拥有内存,因此没有仔细阅读的文档,你不能告诉谁负责所有权)。

In C++ ownership is documented by the type a RAW pointer is wrapped inside thus in a good (IMO) C++ program it is very rare [RARE not NEVER] to see RAW pointers passed around (as RAW pointers have no inferred ownership thus we can not tell who owns the memory and thus without careful reading of the documentation you can't tell who is responsible for ownership).

相反,很少看到存储在类中的RAW指针每个RAW指针存储在自己的SMART指针封装内。 ( NB:如果你没有一个对象,你不应该存储它,因为你不知道什么时候会超出范围并被销毁。)

Conversely it is rare to see RAW pointers stored in a class each RAW pointer is stored within its own SMART pointer wrapper. (N.B.: If you don't own an object you should not be storing it because you can not know when it will go out of scope and be destroyed.)

所以问题:


  • 人们遇到什么类型的所有权语义?

  • 使用什么标准类来实现这些语义?

  • 您觉得哪些情况有用?

让每个答案保持1种语义所有权,以便单独投票。

Lets keep 1 type of semantic ownership per answer so they can be voted up and down individually

概念上来说,智能指针很简单,而且天真的实现很容易。我已经看到了很多尝试的实现,但是它们总是以某种方式被破坏,这对于临时使用和示例是不明显的。因此,我建议总是使用经过良好测试的智能指针从图书馆,而不是滚动自己的。 std :: auto_ptr或其中一个boost智能指针似乎涵盖了我的所有需求。

Conceptually smart pointers are simple and a naive implementations are easy. I have seen many attempted implementations, but invariably they are broken in some way that is not obvious to casual use and examples. Thus I recommend always using well tested "Smart Pointers" from a library rather than rolling your own. std::auto_ptr or one of the boost smart pointers seem to cover all my needs.

单人拥有该对象。

但允许转让所有权。

Single person owns the object.
But transfer of ownership is allowed.

用法:

======

这允许您定义显示所有权显式转移的接口。

Usage:
======
This allows you to define interfaces that show the explicit transfer of ownership.

单个人拥有该对象。

不允许转让所有权。

Single person owns the object.
Transfer of ownership is NOT allowed.

用法:

======

用于显示显式所有权。

对象将被销毁通过析构函数或明确复位时。

Usage:
======
Used to show explicit ownership.
Object will be destroyed by destructor or when explicitly reset.

多个所有权。

这是一个简单的引用计数指针。当引用计数达到零时,对象被销毁。

Multiple ownership.
This is a simple reference counted pointer. When reference count reaches zero object is destroyed.

用法:

======

对象可以具有在编译时无法确定的多个生命周期。

Usage:
======
When object can have multiple owers with a lifetime that can not be determined at compile time.

与shared_ptr< T>一起使用。

在可能发生指针循环的情况下。

Used with shared_ptr<T>.
In situations where a cycle of pointers may happen.

用法:

======

用于在仅循环保持共享引用计数时停止保留对象的循环。

Usage:
======
Used to stop cycles from retaining objects when only the cycle is maintaining a shared refcount.

推荐答案

对我来说,这3种类型涵盖了我的大部分需求:

For me, these 3 kinds cover most of my needs:

shared_ptr - 引用计数,当计数器达到零时解除分配

shared_ptr - reference-counted, deallocation when the counter reaches zero

weak_ptr - 与上面相同,但它是 shared_ptr 的一个奴隶,不能释放

weak_ptr - same as above, but it's a 'slave' for a shared_ptr, can't deallocate

auto_ptr - 当创建和释放发生在同一个函数内部时,或者对象必须被认为是一个所有者。当你把一个指针赋给另一个指针时,第二个指针会从第一个指针偷取对象。

auto_ptr - when the creation and deallocation happen inside the same function, or when the object has to be considered one-owner-only ever. When you assign one pointer to another, the second 'steals' the object from the first.

我有自己的实现,但它们也可以在 Boost

I have my own implementation for these, but they are also available in Boost.

我仍然通过引用传递对象( const 可能),在这种情况下,被调用的方法必须假设对象只在调用期间是活着的。

I still pass objects by reference (const whenever possible), in this case the called method must assume the object is alive only during the time of call.

还有另一种指针,我使用,我调用 hub_ptr 。它是当你有一个对象必须可以从嵌套在其中的对象访问(通常作为一个虚拟基类)。这可以通过传递 weak_ptr 来解决,但它本身没有 shared_ptr 。因为它知道这些对象不会比他活得更长,它传递一个hub_ptr给他们(它只是一个模板包装器到一个常规的指针)。

There's another kind of pointer that I use that I call hub_ptr. It's when you have an object that must be accessible from objects nested in it (usually as a virtual base class). This could be solved by passing a weak_ptr to them, but it doesn't have a shared_ptr to itself. As it knows these objects wouldn't live longer than him, it passes a hub_ptr to them (it's just a template wrapper to a regular pointer).

这篇关于智能指针:或谁拥有你的宝贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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