完成对象还是子对象? [英] Complete object or subobject?
问题描述
C ++ 14标准说:
The C++14 Standard says:
子对象可以是成员子对象,基类子对象或数组元素。不是任何其他对象的子对象的对象称为完整对象。 (§1.8(2))
A subobject can be a member subobject, a base class subobject, or an array element. An object that is not subobject of any other object is called a complete object. (§1.8(2))
对我来说,'可以'是不是一个隐含的if除非'。为了提供一个例子,在下面的代码片段中,是指向一个完整的对象还是子对象?
It is not obvious to me whether the 'can be' is meant as an implicit 'if and only if'. To provide an example, in the snippet below, is r a reference to a complete object or to a subobject?
#include <iostream>
int main(){
int i=2;
unsigned char & r=reinterpret_cast<unsigned char&>(i);
std::cout<<(int)r<<"\n";
}
As r表示对象表示中的unsigned char,r应引用对象:
As r refers to an unsigned char in the object representation , r should refer to an object:
类型T的对象的对象表示是类型T的对象占用的N个unsigned char对象的序列,...(§3.9(4))
The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, ... (§3.9(4))
非常清楚i的第一个字节是什么:
1)没有对象,
2)一个完整的对象,
3)一个子对象
edits: Could you please be very clear about what the first byte of i is: 1) no object at all, 2) a complete object, 3) a subobject
只有这三种可能性。
推荐答案
em>作为下列之一:成员子对象,基类子对象或数组元素。
The sentence is defining the term subobject as being one of: a member subobject, a base class subobject, or an array element.
您的代码段与子对象无关。 r
是引用,而不是对象。此外,它甚至不引用一个对象,它只是将 i
的第一个字节混淆。
Your snippet has nothing to do with subobjects. r
is a reference, not an object. Moreover, it doesn't even refer to an object, it simply aliases the first byte of i
.
[intro.object]:
From [intro.object]:
通过定义(3.1)创建对象 > / new>
An object is created by a definition (3.1), by a new-expression (5.3.4), when implicitly changing the active member of a union (9.3), or when a temporary object is created (4.4, 12.2).
i
是由定义创建的对象。因为 int
不是类或数组类型,它没有子对象。对象表示,构成 i
的存储的 unsigned char
的底层数组不是对象 - 在上述任何上下文中创建。定义对象表示的措辞是核心问题1701 (h / t TC )。
i
is an object created by a definition. As int
is not a class or array type, it has no subobjects. The object representation, the underlying array of unsigned char
that constitutes the storage of i
, is not an object - it's not created in any of those contexts described above. The wording of the definition object representation is the subject of core issue 1701 (h/t T.C.).
这篇关于完成对象还是子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!