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.
#include <iostream> using namespace std; class Base { public: // compiler "declares" constructor }; class A { public: // User defined constructor A() { cout << "A Constructor" << endl; } // uninitialized int size; }; class B : public A { // compiler defines default constructor of B, and // inserts stub to call A constructor // compiler won't initialize any data of A }; class C : public A { public: C() { // User defined default constructor of C // Compiler inserts stub to call A's construtor cout << "C Constructor" << endl; // compiler won't initialize any data of A } }; class D { public: D() { // User defined default constructor of D // a - constructor to be called, compiler inserts // stub to call A constructor cout << "D Constructor" << endl; // compiler won't initialize any data of 'a' } private: A a; }; int main() { Base base; B b; C c; D d; return 0; } |
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- When does compiler create default and copy constructors in C++?
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Inheritance and constructors in Java
- When are Constructors Called?
- Constructors in C++
- C++ | Constructors | Question 2
- C++ | Constructors | Question 3
- C++ | Constructors | Question 4
- C++ | Constructors | Question 5
- C++ | Constructors | Question 6
- C++ | Constructors | Question 7
- C++ | Constructors | Question 8
- C++ | Constructors | Question 9
- C++ | Constructors | Question 10
- C++ | Constructors | Question 17
- C++ | Constructors | Question 12
- C++ | Constructors | Question 13
- C++ | Constructors | Question 14
- C++ | Constructors | Question 15
- C++ | Constructors | Question 16
Improved By : Shaiwal Sharma

