数组元素是不完整的对象吗? [英] Are array elements incomplete objects?

查看:69
本文介绍了数组元素是不完整的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关静态类型的最新问题之后,我再次考虑了数组.我一直以为数组元素只是普通的对象,但是现在我想知道:

After a recent question on static typing, I was thinking about arrays again. I always thought that array elements are just ordinary objects, but now I wonder:

数组的元素本身是否是完整的对象,或者它们是不完整的,而唯一的完整对象是数组本身?

Are the elements of an array complete objects in their own right, or are they incomplete, and the only complete object is the array itself?

如果是这种情况,那么(new T[N])[0]的完整对象是什么,它的类型是什么?

If this were the case, then what is the complete object of (new T[N])[0], and what is its type?

如果元素不完整,是否意味着存在无法静态知道其类型的完整对象? (这与基本/衍生业务不同:new Derived 的类型是静态已知的,尽管可能永远不会记录在该表达式之外.)

If the element is incomplete, then does that mean that there are complete objects whose type cannot be known statically? (This is different from base/derived business: the type of new Derived is known statically, though it may never be recorded outside that expression.)

推荐答案

数组元素不是完整的对象. C ++ 11,[对象简介]§2:

An array element is not a complete object. C++11, [intro.object]§2:

对象可以包含称为子对象的其他对象.子对象可以是成员子对象(9.2),基类子对象(子句10)或数组元素.不是其他任何对象的子对象的对象称为完整对象.

Objects can contain other objects, called subobjects. A subobject can be a member subobject (9.2), a base class subobject (Clause 10), or an array element. An object that is not a subobject of any other object is called a complete object.

至于(new T[N])[0].完整对象是由 new-expression (T的数组)创建的对象.根据[expr.new]§1,我会说它的类型是"TN个元素的数组":

As for (new T[N])[0]. The complete object is the object created by the new-expression (an array of T). I'd say its type is "array of N elements of T", as per [expr.new]§1:

new-expression 尝试创建要对其应用 type-id new-type-id 的对象.

The new-expression attempts to create an object of the type-id or new-type-id to which it is applied.

这是从T[N]构造的 new-type-id .

请注意,在创建数组时, new-expression 返回一个指向该数组初始元素的指针,而不是指向数组本身的指针.这意味着在这种情况下, new-expression 的类型不是其创建的完整对象的类型.换句话说,无法以任何方式访问完整对象的类型.

Note that when creating arrays, a new-expression returns a pointer to the initial element of that array, not to the array itself. Which means that in such case, the type of the new-expression is not the type of the complete object it creates. In other words, the complete object's type cannot be accessed by any means.

这篇关于数组元素是不完整的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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