原始C ++指针是第一类对象吗? [英] Are raw C++ pointers first class objects?

查看:220
本文介绍了原始C ++指针是第一类对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据维基百科

一个对象在它的时候是一等的:

An object is first-class when it:


  • 可以存储在变量和数据结构中

  • 可作为参数传递给子程序

  • 可作为子程序的结果返回

  • li>
  • 具有内在的身份(独立于任何给定名称)

  • can be stored in variables and data structures
  • can be passed as a parameter to a subroutine
  • can be returned as the result of a subroutine
  • can be constructed at runtime
  • has intrinsic identity (independent of any given name)

有人曾经告诉我,不是第一类对象,而智能指针像std :: auto_ptr是。但对我来说,C ++中的一个原始指针(对象或函数)在我看来似乎满足了上面所述的条件作为第一个类对象。我缺少什么?

Somebody had once told me that raw pointers are not first class objects while smart pointers like std::auto_ptr are. But to me, a raw pointer (to an object or to a function) in C++ does seem to me to satisfy the conditions stated above to qualify as a first class object. Am I missing something?

推荐答案

实际上两者 - 指针是FCO和指针不是FCO我们需要在各自的上下文中进行声明。 (FCO - First Class Object)

Actually both - "Pointers are FCO" and "Pointers are not FCO" - are correct. We need to take the statements in the respective context. (FCO - First Class Object)

当我们谈到C ++中的指针时,我们谈论的是一种存储一些其他数据的地址的数据类型。现在是否这是FCO,取决于我们如何设想使用它。不幸的是,这种使用语义不是内置在C ++中的指针。

When we talk of a 'pointer' in C++, we are talking of a data type that stores the address of some other data. Now whether this is FCO or not, depends really on how we envisage to use it. Unfortunately, this use semantics is not built-in for Pointers in C++.

如果我们使用指针只是为了指向数据,它将满足FCO。但是,如果我们使用指针来保存数据,那么它不能再被认为是FCO,因为它的副本和赋值语义不起作用。这种资源处理指针(或更直接地称为原始指针)是我们在智能指针研究的上下文中的兴趣。这些不是FCO,而相应的智能指针。相比之下,仅跟踪指针将继续满足FCO的要求。

If we are using pointers merely to 'point' to data, it will satisfy the requirements of being an FCO. However, if we use a pointer to 'hold' a data, then it can no longer be considered an FCO as its copy and assignment semantics do not work. Such 'resource handling' pointers (or more directly referred as 'raw pointers') are our interest in the context of Smart Pointer study. These are not FCO while the corresponding Smart Pointers are. In contrast, mere tracking pointers would continue to meet the requirements for an FCO.

下面的现代C ++设计一书很好地阐明了这一点。

The following paragraph from "Modern C++ Design" book nicely elucidates the point.

对于智能指针:


具有值语义的对象是一个
对象,您可以复制并分配
。类型int是
一个第一类对象的完美示例。您可以创建,
复制,并自由更改整数值
。用于
在缓冲区中迭代的指针也具有值
语义 - 您初始化它以将
指向缓冲区的开头,并且
将其压缩直到到达结束。
一路上,您可以将其值
复制到其他变量以保持临时
结果。

An object with value semantics is an object that you can copy and assign to. Type int is the perfect example of a first-class object. You can create, copy, and change integer values freely. A pointer that you use to iterate in a buffer also has value semantics—you initialize it to point to the beginning of the buffer, and you bump it until you reach the end. Along the way, you can copy its value to other variables to hold temporary results.

使用指针保存值
分配了新的,但是,故事
是非常不同的。一旦你写了

With pointers that hold values allocated with new, however, the story is very different. Once you have written

Widget * p = new Widget;
变量p不仅指向,而且还拥有为Widget对象分配的内存
。这个
是因为以后你必须发出delete
p,以确保Widget对象被
销毁并释放它的内存。
如果在行后面只有
显示你写

Widget* p = new Widget; the variable p not only points to, but also owns, the memory allocated for the Widget object. This is because later you must issue delete p to ensure that the Widget object is destroyed and its memory is released. If in the line after the line just shown you write

p = 0; //指定别的东西到p
你失去了以前由p指向的对象的所有权,你
没有机会再次抓住
。你有一个资源泄露,
和资源泄漏从来没有帮助。

p = 0; // assign something else to p you lose ownership of the object previously pointed to by p, and you have no chance at all to get a grip on it again. You have a resource leak, and resource leaks never help.

我希望这澄清。

这篇关于原始C ++指针是第一类对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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