The 3rd Chapter Data semanticscalculate the sizeof result for the following code:
Class X{};class Y:public virtual x{};class z:public virtual X{};class a:public Y, public z{};
None of the classes in the above x,y,z,a contain significant data and represent only inheritance, so each class is considered to be 0 in size. The idea is wrong. Even if the size of Class X is not 0.
An empty class such as:
sizeof (X) = 1class x{};
In fact
It is not empty, it has an implicit 1 byte, which is a char inserted by the compiler, which allows the class object to be assigned a unique address in memory:
X A, b;if (&a = = &b) cerr << "yipes!" << Endl;
Both Y and Z are 4 (tested on g++4.8), and the size is related to the machine and to the compiler. In fact,the size of Y and Z is affected by three factors:
1. The additional burden imposed by the language itself (overhead)when the language supports virtual base classes, there are additional burdens. In derived class, this extra burden is reflected in some form of pointer, it either points to the virtual base class Subobject, or to a related table, and the table holds the address that is not the virtual base class Subobject, which is its offset.
2. The compiler provides optimized processing for special casesThe 1byte of the virtual base class X Subobject also appears on Class Y and Class Z, which is traditionally placed at the end of the fixed part of the derived class. Some compilers will mention the empty virtual base class for special support.
3. Alignment RestrictionsOn most machines,The size of the cluster structure is limited by alignment, allowing them to be accessed more efficiently in memory. Alignment adjusts the value to an integer multiple of 4 or 8.
Empty Virtual class has become a unique term for C + + OO design, which provides a virtual interface that does not define any data, some new compilers (such as g++4.8, because Lippmanz this book was written in 2000) This provides a
Special Handling. Under this strategy,
an empty virtual base class is considered the first part of the derived class object, meaning that it does not cost any extra space, which saves the 2nd 1bytes above (since there are members, You do not need a char that was originally inserted for empty class, and it is no longer necessary to fill the 3rd 3bytes. Only the 1th added extra burden, under which the Y and Z sizes are 4 instead of 8 (earlier compilers are 8).
The potential differences between the compilers illustrate the evolution of the C + + object model, which provides a solution to the general situation, and when the special case is gradually excavated, various attempts (attempt errors) are introduced to provide optimized processing. If successful, the heuristic approach is promoted to a universal strategy, and merge across a variety of compilers. It is considered a standard, and it becomes part of the language over time. Virtual function table is an example. Another example is the "named Return value (NRV) optimization" introduced in chapter two
So what's the size of Class A? Obviously, it depends on the compiler in a way. A virtual class Subobject only has an entity in derived class, no matter how many times it appears in the Class inheritance system! The size of Class A is determined by the following points:
1. The only class X entity that is shared, the size is 1bytes
2. The size of base class Y minus the size "configured for virtual base class X", the result is 4bytes. Base class Z is also 4bytes and adds up to 8bytes.
3. Number of alignment (if any) of Class A. The sum of the preceding three items indicates that the size before adjustment is 9bytes.class a must be adjusted to the 4bytes boundary. So we need to fill 3bytes, the result is 12bytes.
If you consider a compiler that "specifically handles the empty virtual base class", the size of base class Y is 4bytes, and base class Z is 4bytes in size, so the size of Class A is 8bytes.If you store one (or more) data members in virtual base class X, the two compilers ("with special handling" and "no special handling") produce identical object layouts.
When there is no data in class X, the empty base class does not take extra space, and the result of sizeof for class X,y,z,a is as follows:
When a data char c is stored in class X, the result is as follows (two compiler results are the same):
A class data members, in general, can show the class's state in the execution of the program. Nonstatic data members place the "individual class object" of interest, and the static data members place the "whole class" of interest.
The C + + object model nonstatic data members as much as possible with the optimization of spatial optimization and access speed, and maintains compatibility with the C-language struct data configuration. It stores data directly in every class Object. For inherited nonstatic data members (either virtual or nonvirtual base class). But there is no compulsion to define their sequence, as
The static data members are placed in a global data segment of the program without affecting the size of the individual class objectIn the program, regardless of how many objects (by direct or indirect derivation) the class is produced.
static data members will always have only one copy of the entity(even if the class does not have any object entities, its static data members also exist). However, the static data members of a template class behave slightly differently.
Each class object must therefore have enough size to accommodate all of its nonstatic data members. Sometimes it can be surprising, because it may be bigger than you think, because:
1. Additional data members that are automatically added by the compiler to support some language features (mainly various virtual features)
2. Because of the need for alignment (border adjustment)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + object Model--data semantics (chapter III)