1. Examples of programs with errors
Class Foo{public:int M_nval; Foo *m_pnext;}; void Foo_bar (void) {Foo Bar; if (Bar.m_nval | | bar.m_pnext) {int i; i++; }}
(1) The correct program semantics is to require Foo to have a default constructor, which can initialize members to 0.
(2) The code above does not synthesize a default constructor, even if the compiler needs to synthesize a constructor, and only perform the actions required by the compiler, without initializing the members to 0.
(3) The correct method is that the programmer provides a default constructor and initializes the members.
2. Section 12.1 of C + + standard[iso-c++95]
For class X, there is no user-declared constructor, there will be a default constructor is implicitly (implied) declaration, the constructor is trivial (useless).
3. Member class object with "default constructor"
(1) A class is not constructor, but there is a member object, the latter has the default constructor, then this class implicit default constructor is Nontrivi Al The compiler synthesizes a default constructor only when constructor really needs to be tuned.
(2) To avoid synthesizing multiple default constructor in different compilation modules, the synthesized default constructor, copy constructor, destructor, assignment copy operator are synthesized in inline form. When inline is not suitable, a explicit non-inline static entity is synthesized.
(3) Case study
Class Foo {Public:foo ();}; Class Bar{public:foo M_foo; int m_pval; };void Func (void) {bar bar;//Bar::m_foo This needs to be initialized}//the synthesized default constructorinline bar::bar (void) {//Call class Foo's default constructor. Initializing Bar::m_foo is the responsibility of the compiler. M_foo. Foo::foo (); M_pval will not be initialized. Is the responsibility of the programmer. }//exists user-declared constructor,//constructor will be expanded, before user code, in "member declaration order" to invoke the necessary default Constructorsbar::bar (Voi d) {M_foo. Foo::foo (); M_pval = NULL;}
4. Base class with "default constructor"
(1) A class without constructor is derived from the base class with default constructor, then the default constructor of this derived class is nontrivial and requires To be synthesized. The default constructor of base class will be called. For the successor class, the synthesized constrcutor is equivalent to user-declared constrcutor.
(2) There are multiple constructor, but when there is no default constructor, the compiler expands each of the existing constructor, adding the required member default constructor without synthesizing the default Constructor
(3) Call the base class constructor first, then call the default constructors of member class objects.
5. Class with a "Vritual function"
(1) In both cases, you also need to synthesize the default constructor
A. Class declares (or inherits) a virtual function.
B. class is derived from an inherited string chain which has one or more virtual base class.
(2) Case study
Class widget{public:virtual void Flip (void) = 0;}; void Flip (const Widget &widget) {//This operation is overridden, (*widget.vptr[1]) (&widget)//"1" represents the index of Flip () in VTBL. &widget represents the this pointer to the "one of the called Flip () function entities". Widget.flip ();} void Func (void) {Bell B; Bell, Whistle inheritance Widget Whistle W; Flip (b); Flip (w);}
Two scaling operations occur during compilation:
A. A vtbl is generated by the compiler, placing the address of the virtual functions class.
B. In each class object, a vptr is synthesized by the compiler, placing the VTBL address.
For each user-delcared constructor of class, the compiler adds some code to do both of these things.
Without any constructor, the compiler will synthesize Defalut constructor to properly initialize Vptr.
6. Class with a "virtual base class"
(1) The implementation method of virtual base class has great difference between different compilers. But the common denominator of each implementation is that the virtual base class must be positioned in each of its derived class object to be ready during execution.
7. Summary
(1) Four cases, the compiler will synthesize a default constructor for the class that does not declare constructor.
(2) the C + + standard calls those compounds the implicit nontrivial default constrcutors.
(3) The synthesized constructor can only meet the needs of the compiler, with the help of "calling member object or base class default constructor", "initializing the virtual function machine for each object" System or virtual base class mechanism ".
(4) For non-existent four kinds of cases, there is no constructor class, has the implicit trivial Defatule constructor, in fact, will not be synthesized.
(5) In the synthesized default constructor, only the base class Subobjects and member class objects are initialized, and the other nonstatic data members are not initialized.
Construction operation of 2.1 Default constructor