********************************************\
| 欢迎转载, 但请保留作者姓名和原文链接, 祝您进步并共勉! |
\********************************************/
C++对象模型(10) - 3.2 Data Member Layout
作者: Jerry Cat
时间: 2006/11/15
链接:
http://www.cppblog.com/jerysun0818/archive/2006/11/15/15192.html
3.2 Data Member Layout
class Point3d {
public:
// ...
private:
float x;
static List<Point3d*> *freeList;
float y;
static const int chunkSize = 250;
float z;
};
the nonstatic data members are set down in the order of their declaration(按声明的顺序) within each class object (any intervening static data members, such as freeList and chunkSize, are ignored). In our example, then, each Point3d object consists of three float members in order: x, y, z. The static data members are stored in the program's data segment independent of individual class objects.
The Standard requires within an access section (the private, public, or protected section of a class declaration) only that the members be set down such that "later members have higher addresses within a class object" (Section 9.2 of the Standard). That is, the members are not required to be set down contiguously.(可以不连续但必须从低到高)
What might intervene between the declared members? Alignment constraints on the type of a succeeding member may require padding. This is true both of C and C++, and in this case, the member layout of the two languages is in current practice the same.(对齐)
虚表指针在哪儿? Traditionally, it has been placed after all the explicitly declared members of the class. More recently, it has been placed at the beginning of the class object. The Standard, by phrasing the layout requirement as it does, allows the compiler the freedom to insert these internally generated members anywhere, even between those explicitly declared by the programmer.
In practice, multiple access sections are concatenated together into one contiguous block in the order of declaration.编译器帮你同类项合并 No overhead is incurred by the access section specifier or the number of access levels. For example, declaring eight members in one access section or eight separate access sections in practice results in the same-sized objects.
posted on 2006-11-15 23:34
Jerry Cat 阅读(940)
评论(0) 编辑 收藏 引用