The 1th chapter on the object
1. The main additional burden of C + + in layout and access time is caused by virtual, including:
A, virtual function mechanism, introducing vptr and VTBL to support an efficient "execution-time binding"
B, virtual base class, to achieve "multiple occurrences of the base class in the inheritance system, there is a single and shared instance"
C, multiple inheritance, the derivation class and the second and subsequent base class conversion between
2, "type of pointer" teaches the compiler how to interpret the memory content in a particular address and its size (the void* pointer can only hold an address, and cannot manipulate the object pointed to) 3, C + + through class pointers and references to support polymorphism, pay the price is the additional indirection. They support polymorphism because they do not throw any "type-related memory delegate operations (Type-dependent Commitment)" in memory, and are changed only by the "size and content interpretation" of the memory they point to. |
The 2nd chapter on the semantics of structural functions
The 3rd Chapter data semantics
1, class object size affected by three factors
A, virtual Base, and virtual function vptr effects
B, EBO (Empty Base C Lass Optimize) empty base class optimization processing, EBC (empty base class) occupies one byte, other derived classes that contain data members derived from EBC, only calculate the size of their own data members, not affected by the EBC one byte
C, Alignment byte Alignment
2, nonstatic data members are arranged in class object in the same order as they are declared, and any intervening static data Members will not be placed in the layout. 3, static member variable static data members
A, stored in the program's data segment
B, through pointers and objects to access member, exactly the same, regardless of inheritance or virtual relay There is only one instance of the global presence
C, static constant members can be initialized directly on the class definition, and ordinary static constant members can only be initialized at the global scope of the. O Compilation unit
4, non-static member variable nonstatic data members
A, the offset of each nonstatic data member can be learned at compile time no matter how complex the derivation is, it is the same. Accessing a nonstatic data member by object is the same as the efficiency and access to a C struct member.
B, from object access obj.x and pointer access pt->x have and differences?
When there is a virtual base class in the inheritance chain, the lookup of the member variable of the virtual base class is deferred to the execution period, and the portion of the virtual base class is found based on virtual class offset, which is slightly less efficient
&N Bsp (data access for member variables does not vary by the this pointer)
the details of the member variable are described in C + + memory distribution |
4th Chapter Function semantics
1, one of the design criteria of C + +: Nostatic member function must have at least the same efficiency as the general nonmember function.
A, rewrite the function prototype, add the this pointer in the parameter
B, for each "nonstatic data member access operation" is replaced by the this pointer to access
C, the member function is rewritten as an external function, "mangling" processing (not to be processed with extern "C")
2. overriding (override), overriding (overload), hiding (hide) differences
overloading means that different functions use the same function name, but the number or type of arguments for a function is different. When calling, different functions are distinguished according to the parameters of the function. Overrides (also called overrides) are re-implemented in a derived class by re-implementing the virtual function in the base class (note that it is a virtual function). That is, the function name and parameters are the same, but the implementation of the functions of the body is not the same. Hiding refers to a function in a derived class that masks a function of the same name in a base class. Hiding and the other two concepts appear to be very similar, it is difficult to distinguish, in fact, their key difference is the implementation of polymorphism.
3. Static member function member functions
A. Non-static members cannot be accessed
B, cannot be declared as const, volatile, or virtual
C, parameter does not have this
D, can not use object access, direct class name:: Static member function Access
4, C + + polymorphic (polymorphism) means "a public base class pointer (or reference), addressing a derived class object" 5, vtable virtual function table must be learned during the compilation, its function number, location and address is fixed, completely controlled by the compiler, during the execution of no modification allowed. 6, vtable content:
A, virtual class offset (with virtual base class only)
B, Topoffset
C, TypeInfo
D, inheriting the virtual function instance declared by the base class, or the virtual function overriding the base class (override)
E, new virtual function (or placeholder for pure virtual function)
7, Execution period virtual function call step
A. Find VTBL through Vptr
B. Adjust the this pointer by Thunk technology and Topoffset (because member variables may be called in member functions)
C. Find the members of the virtual base class shared part through virtual class offset
D. Perform the function of the corresponding slot in the VTBL
8, multiple inheritance, a derived class will have n-1 an additional vtbl (one may have n or more than VTBL, see if there is a virtual base class), it shares vtbl with the first parent class, will modify the other parent class VTBL 9. Function performance
Inline Member > (nonmember Friend, Static Member, nonstatic Member) > virtual Member > Virtual Member (Multiple inheritance) > virtual Member (dummy inheritance)
10, inline on the compiler is only a request, not a command. Local variables in inline + have expression parameters--lots of temporary variables--Program scale skyrocket |
5th chapter Construction, Destruction, copy semantics
1. Destructors cannot be defined as pure virtual functions, and the destructor of each subclass is called by the compiler to extend the call to the destructor of the base class and the destructor of the base class in the upper layer. Because the lack of any definition of a destructor for a base class causes the link to fail. (In fact, pure virtual functions can be defined as well as static calls-class name:: function, but the C + + standard is pure virtual function is not defined) 2. The order of construction with data members under the inheritance system
A, virtual base class constructors, from left to right, from top to bottom. It differs from a non-virtual base class: It is called by the bottommost subclass.
B, non-virtual base class constructors, called from left to right in the order in which the base class is declared. It differs from the virtual base class: It is called by the direct and so on.
C, if there is a virtual table pointer in the class, set the Vptr initial value, if there is a new virtual function or overwrite the base class virtual function, modify the VTBL content (actually vtble at compile time, the class definition has been established)
D, member variables are initialized in the order in which they are declared
E, in the constructor, the user-defined code is finally executed
3, if the class does not define a destructor, then only if the class contains a member object or a base class containing a destructor, the compiler will automatically synthesize one out 4. The sequence of destructors is exactly the opposite of the order of constructors, if it is for a polymorphic destructor, set to virtual destructor 5. Do not call virtual functions in constructors and destructors |
6th. Semantics in the implementation period
1. Postpone the definition of the variable as much as possible, avoid unnecessary construction and destruction (although the C + + compiler will try to ensure that it is constructed when the variable is called, delaying the definition of the variable will make the code good to read) 2. Global class variables are placed in the data segment and set to 0 during compilation
GOOGLE C + + programming Specification:
It is forbidden to use static or global variables of class type, only the pod static variable (Plain old data) and the native datatype are allowed. Because they cause hard-to-find bugs and indeterminate construction and destructor call sequences
Solve:
Change to the static function, produce a local static object
3, if an expression produces a temporary object, then the complete expression should be evaluated before destroying the temporary objects created. There are two exceptions: 1) The temporary object is refer as a reference to another object, 2) The object is used as part of another object, and the other object has not been disposed. |
The 7th chapter stands at the tip of the object model
1, for Rtti support, add a type_info slot in the VTBL 2, dynamic_cast than static_cast to spend more performance (check Rtti release match, pointer offset, etc.), but better security. 3. Apply dynamic_cast:1 to the reference) successfully, or 2) throw Bad_cast exception, apply to pointer: 1) Success, 2) return 0 pointer. 4, using typeid () to judge, legal and then dynamic_cast, so you can avoid the reference operation caused by the bad_cast exception: if (typeid (rt) = = typeID (RT2)) .... However, if RT and Rt2 itself is legally compatible, it will lose a typeid operation performance. |
Explore the C + + object model in depth reading notes