智能指针:谁拥有对象? [英] Smart pointers: who owns the object?

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

问题描述

C ++全部与内存所有权有关-又名所有权语义.

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 ++程序中,它很少出现(稀有,而不是从不)来查看原始指针的传递(因为原始指针没有推断的所有权,因此我们无法确定谁拥有内存,因此,如果不仔细阅读文档,您将无法确定谁负责所有权).

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).

相反,很少看到原始指针存储在类中,每个原始指针都存储在其自己的智能指针包装器中. (注::如果您不拥有对象,则不应存储该对象,因为您不知道该对象何时会超出范围并被破坏.)

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.)

那么问题是

  • 人们遇到过哪种类型的所有权语义?
  • 使用哪些标准类来实现这些语义?
  • 您认为它们在什么情况下有用?

让每个答案保留一种语义所有权,以便可以分别对其进行上下投票.

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 implementation is 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. 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 the reference count reaches zero, the object is destroyed.

用法:当一个对象可以有多个赋值者,且其生存期在编译时无法确定.

Usage: When an 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天全站免登陆