A constructor without any arguments or with default value for every argument, is said to be default constructor. What is the significance of default constructor? Will the code be generated for every default constructor? Will there be any code inserted by the compiler to the user implemented default constructor behind the scenes?
The compiler will implicitly declare default constructor if not provided by programmer, will define it when in need. 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 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 default constructor. The compiler needs to insert code to call the default constructors of base class/embedded object.
A Constructor A Constructor C Constructor A Constructor D Constructor
There are different scenarios in which compiler needs to insert code to ensure some necessary initialization as per language requirement. We will have them in upcoming posts. Our objective is to be aware of C++ internals, not to use them incorrectly.
— by Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- When does compiler create default and copy constructors in C++?
- Default Arguments in C++
- Templates and Default Arguments
- Default Methods in C++ with Examples
- Default constructor in Java
- Is body of a Default Constructor blank in C++?
- What are the default values of static variables in C?
- Default array values in Java
- C++ default constructor | Built-in types
- Default arguments and virtual function
- Default Assignment Operator and References
- Some Interesting facts about default arguments in C++
- Constructors in C++
- Does C++ compiler create default constructor when we write our own?
- C++ | Function Overloading and Default Arguments | Question 2
Improved By : Shaiwal Sharma