C + + terms-everything about the default constructor
Default constructor (Defaultconstructor):
The default constructor is the constructor that is used to create the object when no display initial value is provided. If no constructors are provided, C + + automatically provides a default constructor, and the default constructor has no parameters because the declaration does not contain a value. Also, the compiler provides a default constructor when and only if no constructor is defined. When a constructor is defined for a class, the programmer must provide it with a default constructor, and if a non-default constructor is provided, but no default constructor is provided, then invoking the default constructor when initializing the object is an error:
Stack ww;//Error
There are two ways to define a default constructor, one is to have all parameters have default values, one to define another constructor by means of a function overload---a constructor without parameters.
In summary, if the program does not provide any constructors, the compiler will define a default constructor for the program; otherwise, you must provide the default construction Hansu yourself. The default constructor can have no parameters, and if so, you must provide default values for all parameters.
After understanding these, in-depth analysis is entered.
When does the compiler compose a default constructor. When the compiler needs it! The resultant constructor value executes the action required by the compiler. In classes that do not have any constructors, when initializing objects of this class, the compiler synthesizes a default constructor, but this composition does not initialize the member variables. If you want to initialize a member variable in a class, you must proactively provide a default constructor that is displayed.
So, there is a saying: for CLASSX, if there is no user-defined constructor, then there will be a default constuctor is implicitly (implicitly) declared out ... a default that is installed Constructor will be a trivial (useless) constructor ...
And a nontrivial defaultconstructor is what the compiler needs, there are four scenarios in which the compiler will synthesize nontrivial default constructor, There are four scenarios in which the compiler will synthesize Nontrivialdefault constructor. The compiler can only synthesize the default constructor when the compiler needs it.
1, "with Defaultconstructor" Member class object (that is, a member of a class has defalult constructor)
If a class does not have any constructor, but it contains a memberobject, and the latter has the default constructor, then this class's implicit default Construct even nontrivial, The compiler needs to synthesize a defaultconstructor for this class. Note that this class does not have any constuctor is the first premise, if this class has any one constructor, then the compiler will not synthesize the default constructor. At the same time they are synthesized in the form of inline. The synthesized default constructor is only responsible for initializing the Member class object with the default constructor.
Also, if the user defines a default constructor that does not appear in the default constructor for the calling object member, the default constructor for the object member is synthesized into the default constructor for that definition.
2. Base class with Defaultconstructor (class inherited from the default constructor)
If no constructors class is derived from a base class with Defaultconstructor, then the default of this derived class Constructor are considered nontiivaial and therefore need to be synthesized.
In this case, if the designer provides multiple constructors, but none of them defines the default constructor? Within these character-defined constructors, if the parent class's constructor is not called to initialize the parent class member, the parent class's default construcots is added. It does not synthesize a new default constructor, because there is already a user-defined constructor that exists (see the first part to understand). If this subclass exists with the Member class objects with default constructor, then Defalult constructor is also called----at all base class After the constructor is called (for what?). Because there are a few relationships that call the constructor of the subclass before the members of the subclass are initialized
3, "With a Virtualfunction" class
Whether it is an inheritance or a declaration, as long as there is a virtual function in this class, and the lack of a user-defined constructor, the compiler will synthesize a default constructor
4, "with a virtual BaseClass" class
If there are no user-defined functions, but this class inherits from the virtual base class, then the compiler will synthesize a default constructor
Summary: In the above four cases, the compiler must synthesize a default constructor,c++ standard for classes that do not have a life constructor to make those compounds an implicitly useful defaults constructor (implicitnontrivial constructors), but the synthesized constructor only satisfies the compiler's needs. They can complete the task by "invoking the default constructor of the member object or base class" or "initializing its virtual function mechanism or the virtual base class mechanism for an object to be extinguished". As for classes that do not have those four cases without any constructors, we say they have the implicit trivial default constructor (the implicitly useless defaults constructor).
In the synthesized defaultconstructor, only the base class Subobjects and member class objects are initialized. All other members are not initialized. These initialization operations may be necessary for the program, but are not necessary for the compiler.
C + + fundamentals-everything about the default constructor