C ++: the default constructor for Compiler synthesis. The constructor for Compiler synthesis.
There are two common misunderstandings:
1. If no default constructor is defined for any class, it will be merged.
2. The default constructor synthesized by the compiler explicitly sets the default value for each data member in the class.
For the first misunderstanding, no class will be merged into a default constructor by the compiler when no default constructor is explicitly defined.
In the following four cases, the compiler will synthesize the default constructor to meet the compiler's own needs (not to meet the program's needs ).
1. The parent class has a default constructor)
If a class without any constructor is derived from the "one with default constructor" parent class, the default constructor of this derived class is considered "useful (required by the compiler) ", so it needs to be synthesized. It calls the default constructor of the parent class.
If the derived class contains multiple constructors, but does not include the default constructor, the compiler will not synthesize the new default constructor for it, but expand each constructor, install the code of all the Default constructors to be called.
2. Class Members with class types
If Class A contains class type members and the class type has A default constructor, the default constructor of Class A is considered as "useful" and needs to be merged.
// X, Y, and Z all carry the explicit default constructor class X {public: X () ;}; class Y {public: Y ();}; class Z {public: Z () ;}; class A {public: X x; // three class types Y y; Z z Z; A (int ); // comes with A constructor private: int val;}; // The programmer's implementation of the constructor A (as you can see) A: a (int) {val = a;} // After the compiler extends the synthesis (the compiler thinks this should be the case) A: A (int a) {// Insert the code in declared order, call to construct x. x: X (); y. y: Y (); z. z: Z (); // explicit user code val = ;}
Iii. classes with virtual functions
Two scaling actions will occur during compilation:
1. the compiler will generate a virtual table (containing the address of the class's virtual function ).
2. In each class object, an extra virtual table pointer is synthesized by the compiler to point to the relevant virtual table.
4. classes with virtual base classes
The implementation of the virtual base class must meet the requirements of the virtual base class in its "position in the object of each derived subclass" can be properly prepared during the execution period.
Class A {public: int a ;}; class X: public virtual A {public: int x ;}; class Y: public virtual A {public: int y ;}; class Z: public X, public Y {public: int z ;}; // the position of p-> A: a in void fun (X * p) cannot be determined during compilation) {p-> a = 1;} // The True Type of p cannot be determined during compilation (the base class pointer can point to the object of the derived class, and the actual type is the type of the derived class) int main () {fun (new X); // The actual type is X fun (new Z); // The actual type is Z return 0 ;}
The compiler cannot determine the actual offset position of "A: a" accessed by p "in fun (), because the True Type of p is variable, as shown in main () can be X or Z. The compiler must change the code of the access operation so that A: a is delayed until execution.
Fun () can be rewritten as follows by the compiler:
// Compiler Transformation Operation, where vcbA is the pointer generated by the compiler, pointing to the virtual base class Avoid fun (X * p) {p-> vcbA-> a = 1 ;} // vcbA is completed during class Object Construction
In other four cases, if the class does not declare any constructor, they will have an implicit and "useless" default constructor which will not actually be synthesized.
For the second misunderstanding, In the synthesized default constructor, only the base class sub-objects and class type objects will be initialized, while all other non-static members (such as integers, pointers, array, etc.), will not initialize, they should be initialized by the programmer, rather than the compiler.
Note: The default value type is not the initialization of the default structure.