C++|深入理解智能指针

 我来答
舒适还明净的海鸥i
2022-08-01 · TA获得超过1.7万个赞
知道小有建树答主
回答量:380
采纳率:0%
帮助的人:69.7万
展开全部
It’s often the case that you’ll need something that behaves like a pointer, but a built-in pointer type just doesn’t do the job. In those cases, a C++ programmer will use a “smart pointer. ”

我们经常遇到这样的情形:需要某种类似指针的东西,但内建的指针又做不了我们想要做的事情。在这些情形下,C++程序员可以使用一个“智能指针” 。

A smart pointer is a class type that is tricked up to look and act like a pointer but that provides additional capability beyond that provided by a built-in pointer. Generally, a smart pointer uses the capabilities provided by a class’s constructors, destructor, and copy operations to control access to or keep track of what it points to in a way that a built-in pointer cannot.

智能指针是一个类类型,它乔装打扮成一个指针,但额外提供了内建指针所无法提供的能力。通常而言,一个智能指针通过使用类的构造函数、析构函数和复制操作符所提供的能力,来控制(或跟踪)对它所指向的东西的访问,而内建指针在这方面则无能为力。所有智能指针都重载 -> 和* 操作符,从而可以采用标准指针语法来使用它们(一些罕见的智能指针甚至还重载了->* 操作符)。

All smart pointers overload the -> and * operators so that they can be used with standard pointer syntax. (Some rare specimens even go so far as to overload the ->* operator; ) Other smart pointers (in particular, smart pointers used as STL iterators) overload other pointer operators, like ++, --, +, -, +=, -=, and []. Smart pointers are often implemented as class templates so that they may refer to different types of objects. Here’s a very simple smart pointer template that performs a check that it’s not null before use:

另有一些智能指针(尤其是用作STL迭代器的指针)还重载了其他一些指针操作符,包括++、--、 、-、+=、-=以及[ ]等。智能指针通常采用类模板来实现,从而使它们可以指向不同类型的对象。下面是一个非常简单的智能指针模板,它执行一个检查, 确保在被使用之前不为空:

Use of a smart pointer should be straightforward, mimicking the use of a built-in pointer:

智能指针的用法非常简单,酷似内建指针的用法:

The key to this facede is the overloaded operator ->. The -> operator must be overloaded as a member and has a rather unusual property in that it is not “consumed” when it is called. In other words, when we write s->draw(), the compiler recognizes that s is not a pointer but a class object with an overloaded operator -> (that is, that s is a smart pointer). This results in a call to the member overloaded operator, which returns (in this case) a Shape * built-in pointer. This pointer is then used to call Shape’s draw function. If you write this out longhand, you’ll get the following challenging expression: (s. operator ->())->draw(), which contains two uses of the -> operator, one overloaded, one built in.

在这个用法外表之下的关键点在于重载的operator->, 此操作符必须被重载为一个成员函数,并且具有一个非同寻常的性质,即当它被调用时,它并不被消耗掉(consumed),换句话说, 当我们写s->draw()时, 编译器识别出s不是一个指针, 而是一个重载了operator->的类对象(即s是一个智能指针)。这将导致对成员重载的操作符的调用,本例中返回一个Shape*内建指针。然后该指针被用于调用Shape的draw函数。如果采用普通写法来编写代码,将会得到如下具有挑战性的表达式(s.operator->())->draw(), 其中包含了两个->操作符,一个是重载的版本,另一个是内建的版本。

Smart pointers also typically overload operator * as well as operator -> so that they may be used to refer to nonclass types.

除了重载operator->外,智能指针通常还重载operator *, 从而可以用它们指向不是类的类型,如下所示:

Smart pointers are used pervasively in C++ programming, from resource handles to STL iterators, to reference counting pointers, to wrappers around pointers to member functions, and on and on.

智能指针在C++编程中广泛使用,从资源句柄到STL迭代器,再到引用计数指针,再到围绕成员函数的指针的包装器,等等。

ref

《C++ COMMON KNOWLEDGE: ESSENTIAL INTERMEDIATE PROGRAMMING》
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
上海德康电子
2023-06-12 广告
ALPS Bluetooth 模块是一种支持蓝牙协议的无线通信模块,通常用于连接蓝牙设备,如手机、耳机、音箱等。ALPS Bluetooth 模块有多种类型,其中包括:1. UGZZC-G 车载用Bluetooth模块:这是一种内置蓝牙协议... 点击进入详情页
本回答由上海德康电子提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式