问题描述
在有关静态类型的最新问题之后,我再次考虑了数组.我一直以为数组元素只是普通的对象,但是现在我想知道:
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:
至于(new T[N])[0]
.完整对象是由 new-expression (T
的数组)创建的对象.根据[expr.new]§1,我会说它的类型是"T
的N
个元素的数组":
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:
这是从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.
这篇关于数组元素是不完整的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!