C ++ Constructor
1. Default constructor
1. for class X, if there is no user-declared constructor, the default constructor generated by the compiler is useless.
2. The default constructor of the compiler synthesis (expansion) is useful in 4 cases
①. Member class object with default constructor
The Default constructor of the Member class object is called in the default constructor of the synthesis (expansion.
②. Base class with default constructor
The default constructor of the merging (expansion) calls the default constructor of the base class.
③. Class with a virtual funtion
The default constructor of the merging (expansion) class initializes the vptr of the class.
④. Class with a virtual base class
3. misunderstandings
①. Any class will be merged if default constructor is not defined.
②. The default constructor synthesized by the compiler will be explicitly set the default value for each data member in the class.
Ii. Copy constructor
1. In three cases, the content of one object is used as the initial value of another class.
① Explicitly use the content of one object as the initial value of another class object X xx = x;
②. Void foo (X x) when the object is passed to a function as a parameter)
③ When the function returns a class object x = foo () {return xx ;}
If the copy constructor is defined, it will be called in most cases. Otherwise, it is implemented using the so-called default memberwise initialization method internally, and the member class object implements memberwise initialization recursively.
2. the compiler will synthesize copy constructor, that is, the class does not show bitwise copy semantics.
①. When the class contains a member object while the latter class declaration has a copy constructor (conditional synthesis)
②. When the class inherits from a base class and the latter has a copy constructor (explicitly declared or synthesized)
③ When the class declares one or more virtual funtions
④. When the class is derived from an inherited string, one or more virtual base classes are involved.
The following is a brief description of the third point:
We know the two program expansion operations during compilation (with the existence of virtual funtion)
I. added a virtual function table (vtbl) containing the address of each useful virtual function.
II. Place a pointer to the virtual function table (vptr) in each class object
From these two points, we can see that the compiler needs to synthesize a copy constructor in order to properly initialize the vptr.
Class ZooAnimal
{
Public:
ZooAnimal (){}
Virtual ~ ZooAnimal (){}
Virtual void animate (){}
Virtual void draw (){}
};
Class Bear: public ZooAnimal
{
Public:
Bear (): B (5) {memset (this, 0, sizeof (Bear ));}
Virtual void animate (){}
Virtual void draw (){}
Virtual void dance () {}// virtual functions not available in the parent class
};
Bear yogi;
Bear winnine = yogi;
In this case, it is safe to copy the vptr value of yogi to the vptr of winner, that is, the vptr of the two objects is identical.
However
ZoonAnimal franny = yogi;
At this time, franny's vptr cannot be set to point to the virtual table of Bear class. That is to say, the ZoonAnimal copy constructor will explicitly set the vptr of the object to point to the virtual table of the ZoonAnimal class, instead of directly copying the current vptr value from the class object on the right hand side.
Iii. member Initial Value column (member initialization list)
1. member initialization list must be used for compilation.
① When a reference member is initialized
② When a const member is initialized
③ When a constructor of a base class is called and it has a set of parameters
④ When the constructor of member class is called and it has a set of parameters
The initialization sequence is determined by the member declaration sequence instead of the Project sequence in the list. The Compiler inserts the initialization operation in the constructor according to the declaration sequence in the initialization list one by one and before any explicit user code.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.