Default Constructors in C++
A constructor without any arguments or with the default value for every argument is said to be the Default constructor.
What is the significance of the default constructor?
They are used to create objects, which do not have any having specific initial value.
Is a default constructor automatically provided?
If no constructors are explicitly declared in the class, a default constructor is provided automatically.
Will there be any code inserted by the compiler to the user implemented default constructor behind the scenes?
The compiler will implicitly declare the default constructor if not provided by the programmer, will define it when in need. The compiler-defined default constructor is required to do certain initialization of class internals. It will not touch the data members or plain old data types (aggregates like an array, structures, etc…). However, the compiler generates code for the default constructor based on the situation.
Consider a class derived from another class with the default constructor, or a class containing another class object with the default constructor. The compiler needs to insert code to call the default constructors of the base class/embedded object.
A Constructor A Constructor C Constructor A Constructor D Constructor
There are different scenarios in which the compiler needs to insert code to ensure some necessary initialization as per language requirements. We will have them in upcoming posts. Our objective is to be aware of C++ internals, not to use them incorrectly.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.