C++ 存储模型是 C++ 程序实现的物理基础,C++ 对象模型是 C++ 语义实现的逻辑基础。C++ 标准指出:
1.7 The C++ memory model [intro.memory]
The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit. The memory available to a C++ program consists of one or more sequences of contiguous bytes. Every byte has a unique address.
C++ 存储模型的基本存储单元是字节。一个字节应该至少具有足够表示运行时基本字符集的任意成员的大小,并且由实现定义个数的连续位构成。最低有效位称为低序位,最高有效位称为高序位。对于 C++ 程序有效的存储由一个或多个字节连续序列组成。每个字节有唯一的地址。
1.8 The C++ object model [intro.object]
The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage. [Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. ] An object is created by definition (3.1), by a new-expression (5.3.4) or by the implementation (12.2) when needed. The properties of an object are determined when the object is created. An object can have a name (clause 3). An object has a storage duration (3.7) which influences its lifetime (3.8). An object has a type (3.9). The term object type refers to the type which the object is created. Some objects are polymorphic (10.3); the implementation generates information associated with each such object that makes it possible to determine that object's type during program execution. For other objects, the interpretation of the values found therein is determined by the type of the expressions (clause 5) used to access them.
C++ 的程序构造中创建,销毁,引用,访问并操作对象。对象是一块存储区域。【注:无论是否函数占据存储的方式与对象相同,函数都不是对象。】对象按需通过定义(3.1),new 表达式(5.3.4)或由实现创建。当对象创建后,对象的特性被确定。对象可以有名字(章节3)。对象具有存储类型(3.7),并影响它的寿命(3.8)。对象具有类型(3.9)。术语对象类型指出对象创建时使用的类型。一些对象是多态的(10.3);为了在程序运行时能够决定对象的类型,实现为每个这种对象生成相关的信息。对于其他对象,以访问这些对象的表达式(章节5)的类型来决定如何解释它们所存放的值。
Objects can contain other objects, called sub-objects. A sub-object can be a member sub-object (9.2), a base class sub-object (clause 10), or an array element. An object that is not a sub-object of any other object is called a complete object.
对象可以包含被称为子对象的其他对象。子对象可以是成员对象(9.2),基类对象(章节10),或数组元素。不是任何其他对象的子对象的对象称为完整对象。
For every object x, there is some object called the complete object of x, determined as follows:
对每个对象 x,存在被称为 x 的完整对象的对象,判定如下:
If x is a complete object, then x is the complete object of x.
如果 x 是完整对象,则 x 的完整对象是 x。
Otherwise, the complete object of x is the complete object of the (unique) object that contains x.
否则,x 的完整对象是包含 x 的(唯一)完整对象。
If a complete object, a data member (9.2), or an array element is of class type, its type is considered the most derived class, to distinguish it from the class type of any base class subobject; an object of a most derived class type is called a most derived object.
如果完整对象,数据成员或数组元素具有类类型,则其类型被看作最终派生类,以之与此类的任何基类子对象的类型区别;一个最终派生类类型的对象被称为最终派生对象。
Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class sub-objects may have zero size. An object of PODi) type (3.9) shall occupy contiguous bytes of storage.
除非作为位域(9.6),最终派生对象总是具有非零大小并总占用一个以上字节的存储。基类子对象可以具有零大小。PODi)类型(3.9)的对象总是占用连续的字节存储。
i) The acronym POD stands for “plain old data.”
POD 是“老式普通数据”的首字母缩写。
[Note: C++ provides a variety of built-in types and several ways of composing new types from existing types (3.9). ]
【注:C++ 有许多内建类型,并提供几种利用已有类型组合出新类型的方法(3.9)。】