C++ Initializer List
The data members of a class are initialized using an initializer list. The constructor specifies the initialization list of members as a comma-separated list followed by a colon. Here is an illustration of how to initialize the x and y properties of the Point class using the initializer list.
x = 11, y = 17
The code shown above only serves as an illustration of the Initializer list's syntax. In the code above, initializing x and y in the constructor is also a simple process. However, there are some circumstances when using an initializer list instead of initializing data members inside the constructor is necessary. These instances are as follows:
1) For non-static const data members initialization:
Const data members require the use of an Initializer List to be initialized. In the example below, "t" is an initializer list-initialized const data member of the Test class. Const data members are initialized in the initializer list because no memory is allocated specifically for them; instead, they are folded into the symbol table, necessitating their initialization in the initializer list.
Additionally, because it is a parameterized constructor, we do not need to call the assignment operator, which saves us from doing an additional action.
2) For the reference members' initialization:
Initializer List must be used to initialize reference members. In the example that follows, "t" is an initializer list that initialized reference members of the Test class.
3) For member objects that do not have a default constructor to be initialised:
As shown in the example below, class "A" does not have a default constructor, and object "a" of class "A" is a data member of class "B". It is necessary to initialize "a" with an initializer list.
Constructor A is called: Value of i: 50 Constructor B is called
Initializer List is not required if we want to initialize "a" using the default constructor, but it is required if we want to initialize "a" using the parameterized constructor if class A has both default and parameterized constructors.
4) For base class member initialization:
Similar to point 3, only Initializer List can be used to call the base class' parameterized constructor.
Constructor A is called: Value of i: 46 Constructor B is called
5) When a data member's name is the same as a constructor parameter
If the name of the constructor argument and the name of the data member match, then either this pointer or the Initializer List must be used to initialize the data member. The member name and parameter name for A() in the example below are both "i".
6) For purposes of performance:
Instead of assigning values inside the body, it is preferable to initialize all class variables in the Initializer List. Think about the following instance:
The following steps are used by the compiler to generate an object of the MyClass type here.
The constructor of the type is first invoked for "a".
The MyClass() constructor calls the assignment operator of "Type" to assign.
The processes that the compiler takes while dealing with the Initializer List are as follows:
Consider the same code modified to include the Initializer List in the MyClass() constructor.