> When compiler automatically generates default constructor
Speaking of the default constructor in C ++, Let's see when the default constructor will be generated.
1. A class member has a member as a class object, and the member's class contains the default constructor.
The C ++ compiler will help you
This class also generates a default constructor to call the constructor of its member object to complete the initialization of the member. Note that if the member's class does not provide the default constructor
The C ++ compiler will not help you generate the default constructor for this class.
So for the pod type struct, we can think of it as C-style struct.
2. The base class of this class has the default constructor.
The C ++ compiler will also help you generate the default constructor of the derived class to call the default constructor of the base class and complete the initialization of the base class.
If the base class does not provide the default constructor, the C ++ compiler will not generate the default constructor for the derived class.
3. If there is a virtual function in the class, the C ++ compiler will generate the default constructor for you to initialize the virtual table (vftable ).
4. If there is a virtual base class, the C ++ compiler will generate the default constructor for you to initialize the virtual base class table (vbtable ).
> Defaulted constructor in C ++ 11
C ++ 11 adds a default keyword
Struct sometype
{
Sometype () = default; // The default constructor is explicitly stated.
Sometype (othertype value );
};