The default constructor is synthesized only when the compiler needs it.
Generally, the default constructor that is synthesized by the compiler is useless (trivial), with the following exceptions:
(1) member Class Object with "Default constructor"
If a class does not have any constructor, but it contains a member object, and the latter has a default constructor, then the compiler does not synthesize a "class" when constructor really needs to be called. Nontrivial "the default constructor. To avoid the synthesis of multiple default constructor, the solution is to synthesize the default constructor, copy constructor, destructor, assignment copy Operator are done in a inline way. A inline function has a static linkage that is not visible to people outside the file. If the function is too complex to be made into a inline, a explicit non-inline static entity is synthesized.
According to the guidelines "If Class A contains one or more member class objects, then each constructor of class A must invoke the default constructor of each member classes", Even for a user-defined default constructor, the compiler expands it, placing the default associated with each member by the "explicit" in the order in which they are declared before the username code ("Objects in Class") constructor.
class Dcpey { public:Dopey(); ... };
class Sneezy { public:Sneezy(int); Sneezy(); ... };
class Bashful { public:Bashful(); ... };
class Snow_White {
public:
Dopey dopey;
Sneezy sneezy;
Bashful bashful;
// ...
private:
int mumble;
};
Snow_White::Snow_White() : sneezy(1024)
{
mumble = 2048;
}
// 编译器 扩张后的default constructor
Snow_White::Snow_White() : sneezy(1024)
{
// 插 入member class object
// 调用其constructor
dopey.Dopey::Dopey();
sneezy.Sneezy::Sneezy();
bashful.Bashful::Bashful();
// explicit user code
mumble = 2048;
}
(2) "with default constructor" Base Class
If a class that does not have any constructors derives from a base class "with default constructor", then the default constructor of this derived class is considered Nontrivial, and therefore need to be synthesized out.
It is to be noted that the compiler will place these base class constructor before the member object.