Special handling of names (Name Mangling)
MEMBER:
Class bar {public: int tent; ...}
Class foo: public class bar {public: int tent; ...}
After treatment:
Class foo {
PUBLIC:
INT IVAL_3BAR;
INT IVAL_4FOO;
}
Such as function:
Class point {
PUBLIC:
Void X (FLOAT NEWX);
Float x ();
...
}
After processing:
Class point {
PUBLIC:
Void X_5Pointff (Float Newx);
Float x_5pointfv ();
...
}
Code the parameters and function names together, the compiler then reached a limited form of type inspection between different compilation modules ("return type" declaration error has no way to check out).
NonStatic Member Functions (non-static member function)
Need to transform internal conversion, steps:
1. Rewrive the function prototype to add an additional parameter to the Member function to provide an access pipe, so that the Class Object can call the function. This extra parameter is called this pointer:
Point3D Point3d :: Magnitude (Point3D * const this)
If the Member function is const, it becomes:
Point3D Point3d :: Magnitude (const point3d * const this)
2. Change each "access operation to Nonstatic Data Member to access via the THIS pointer.
3. Rewote the Member function into an external function. The "Mangling" process is performed on the function name so that it is a unique communication in the program.
The call operation of the function must also be converted.
Such as: obj.magnitude () becomes: Magnitude_7Point3DFV (& obj);
Static MEMBER FUNCTION (Static member function)
Such as: obj.normalize (); ptr-> normalize (); will be converted to a general NonMember function call,
Like this: normalize () _ 7point3dsfv ();
The main feature of Static Member Functions is that it does not have this pointer, and the secondary feature is all from its main feature:
1. It does not directly access NonStatic Members in its class.
2. It cannot be declared as const, volatile or virtual.
3. It doesn't need to be called via Class Object - although most of it is called like this!
If Class Object is obtained for a certain expression, this expression still needs to be evaluated;
Such as: if (foo () .Objcet_count ()> 1) ...
Converted to: (void) foo (); if (Point3D :: Object_count ()> 1) ...
If a Static Member function is taken, it will be its location in the memory, that is, its address. Since Static Member Function does not have this pointer, its address is not a "pointer to Class Member Function", but a "nonmember function pointer", that is, & point3d :: object_count (); get a value, The type is: unsigned int (*) ();
Instead: unsigned int (Point3D :: *) ();
Virtual MEMBER FUNCTION (Virtual member function)
Such as: ptr-> normalize (); will be converted to: (* Ptr-> VPTR [1]) (this);
A clear call operation will suppress a virtual mechanism, and its resolution mode is the same as the NonStatic Member function. Such as: register float mag = point3d :: magnitude (); // Magnitude is Virtual Function
In C , polymorphism means "The meaning of" a Public base Class ", addressing a derived class object".
Identify if a Class supports polymorphism, the only proper method is to see if it has any Virtual function.
There is such a call: ptr-> z ();
In order to correctly know two points in the execution period:
1. The real type of the object referred to in PTR. This allows us to choose the correct Z () entity;
2. Z () entity location so that I can call it.
In implementation, two Members can be added to each polymorphic Class Object:
1. A string or number indicating the type of Class;
2. A pointer, pointing to a table, the execution period of the Virtual Functions with programs in the form.
In order to find the address of the Virtual Function in the execution period, there is a need for two steps:
1. In order to find the form, each class object is placed on a pointer generated by the compiler, pointing to this form.
2. In order to find the function address, each Virtual Function is assigned a table index value.
Virtual functions under single inheritance:
A Class will only have a Virtual Table. Each Table contains the address of all Active Virtual Functions function entities in its corresponding Class Object. These Active Virtual Functions include:
1. The function entity defined by this Class. It will rewrite a possible existing base class virtual function function entity.
2. Inherit the function entity from Base Class. This is the case where Derived Class decides to do not change the Virtual FUNCTION.
3. A pure_virtual_called () function entity, which can play both Pure Virtual Function's space security role, or it can be used as an exception handler (sometimes used).
Calling Pure Virtual Function, usually ending the procedure. Virtual functions under multiple inheritance:
Virtual Functions supports Virtual Functions in multiple inherits, with complexity around the second and subsequent base classes, and "adjust the THIS pointer must be adjusted during the execution period".
Such as: base2 * PBase2 = new derived; the address of the new DeriveD object must be adjusted to point to its base2 subobject. The following code will produce the following code:
// Transfer to support the second Base Class
Derived * Temp = New Derived;
Base2 * PBase2 = TEMP? TEMP SIZEOF (BASE1): 0;
When the programmer wants to delete the object referred to by PBASE2: The pointer must be adjusted again, in case another point to the start of the DeriveD object again.
Under multiple inheritance, a DeriveD Class contains N-1 additional Virtual Tables, n indicates the number of Base Classes it.
1. A major entity, shared with Base1 (vtbl_derived).
2. A secondary entity, related to BASE2 (vtbl_base2_derived).
Allow a Virtual Function to vary, possibly Base Type, or publicly deived type.
Virtual function under virtual inheritance:
There is a suggestion, do not declare Nonstatic Data Members in a Virtual Base Class.
Point pointers to MEMBER FUNCTION (Pointer-to-Member functions)
Take the address of a nonStatic Data Member, the result is the BYTES location of the Member in the Class layout (plus 1). It is an incomplete value that needs to be bound to an address of a Class Object to be accessed.
Take an address of a nonStatic Member function if the function is NonVirtual, the result is its true address in memory. This value is not complete, it also needs to be bound to an address of a Class Object to call the function by it. All NonStatic Member functions requires the address of the object (indicated by parameter THIS).
A pointer to the Member function, such as:
Double (Point :: * PMF) () = & POINT :: x;
X can be Virtual or NonVirtual. But the results explained are different:
((int) PMF) & ~ 127)? (* PMF) (PTR): (* PTR-> VPTR [(int) PMF] (PTR))
Inline functions (inline function)
Handling the Inline function, there are two phases:
1. Analyze the function definition to determine the "Intrinsic Inline Ability" of the function (essential inline capabilities). The term "intrinsic" (intrinsically, inherent) means "related to the compiler". If the function is determined by its complexity, or due to its construction issues, it is not becoming inline, which will be converted to a Static function. 2. The real Inline function extension is at that point in call, which will bring the evaluation operation (Evaluation) and the management of the temporary object.
Formal parameters:
In general, facing "actual parameters that will bring side effects", usually need to introduce temporary objects.
That is, if the actual parameter is a constant expression, you can complete the evaluation operation before replacing; after the Inline replace, you can "tied" directly. If neither a constant expression, it is not an expression with side effects, then directly replace it.
Local variable:
If the inline function extensions multiple times in a single expression, each extension requires its own set of local variables. If the inline function is expanded multiple times with separate multiple styles, only a set of local variables can be reused (because they are placed in a closed section, there is their own scope).
Such as: single expression: minval = min (VAL1, VAL2) min (foo (), foo () 1);
Separated multiple expressions: minVal_1 = min (VAL1, VAL2); minVal_2 = min (VAL3, VAL4);
The Inline function provides a necessary support for the package that can effectively access Nonpublic data encapsulated in the Class. It is also a safe instead of a large number of #define (pre-processing macro) in the C program - especially if the parameters in the macro have side effects. However, an inline function will generate a large amount of extended code if it is called too many times, so that the size of the program has soared.