Explore All About Initializer Lists In C++ With Examples In Detail.
C++ uses initializer lists to initialize the data members to their values.
Initializer lists are mostly used in specific situations where it is not possible to initialize the values in the constructor body.
=> Click Here For The Free C++ Course.
Table of Contents:
Initializer Lists In C++
The general syntax of the Initializer list is:
constructorName(type value1, type value2):datamember(value1), datamember(value2) { //constructor code }
Some of these situations where the initializer lists are used are listed below:
#1) A Default Constructor is not Provided for the Member Object Class.
When we have another class object as the member of the class and the class of that object doesn’t have default constructor, then we initialize this data member object using the initializer list.
This is shown in the following program.
#include <iostream> using namespace std; class A { int i; public: A(int ); }; A::A(int val) { i = val; cout << "Constructor ::A ; i = " << i << endl; } class B { A a; public: B(int ); }; B::B(int val):a(val) { cout << "Constructor :: B"; } int main() { B obj(10); return 0; }
Output:
Constructor ::A ; i = 10
Constructor :: B
In the above program, the object of class A is a data member of class B. In class A, we have a parameterized constructor but not a default constructor. Thus we need to use initializer list in class B, in order to create class A object.
#2) Initializing Const Data Members.
As const data members can be initialized only once, we initialize them using the initializer list.
We demonstrate this using the following Example.
#include<iostream> using namespace std; class sample { private: const int val; public: sample(int val):val(val) { cout << "Value is " << val; } }; int main() { sample obj(10); }
Output:
Value is 10
In the above program, we have a const as a data member. If we have to initialize this const member to a value, then we do it using the initializer list as shown.
#3) For Initializing Reference Data Types.
Like constants references are immutable. So whenever we need to initialize the reference type data members, we use initializer lists.
In the following program, we initialize the reference data member using the initializer list.
#include<iostream> using namespace std; class sample { private: int &ref_val; public: sample(int &ref_val):ref_val(ref_val) { cout << "Value is " << ref_val; } }; int main() { int ref=10; sample obj(ref); return 0; }
Output:
Value is 10
In this program, we have a reference as a data member and we initialize it using the initializer list.
#4) When Member Name and Parameter have the same Name.
When member names of a class are to be initialized with the parameters and these parameters have the same names as member names, we use initializer list. In our previous topics, we used this pointer for the same situation. The second option is the initializer list.
We have modified the above Example with this pointer to make use of initializer list in the following program.
#include <iostream> using namespace std; class Sample { private: int num; char ch; public: Sample(int num,char ch):num(num),ch(ch){ cout<<"num = "<<num<<endl; cout<<"ch = "<<ch; } }; int main(){ Sample obj(100,'A'); return 0; }
Output:
num = 100
ch = A
In this program, we pass parameters to initialize the data members having the same names. In this case, we use the initializer list for this purpose.
#5) To Improve Performance
Every time we initialize the data members in the body of the constructor, a temporary object is created for the data member while assigning values. Once the assignment is done, this temporary object is deleted. This is actually an overhead.
If we use the initializer list, on the other hand, a copy constructor is used for assigning values. This significantly improves performance.
Conclusion
Thus in this tutorial, we have learned about the initializer lists along with many situations in which we need to use them in C++ programming.
Though we need not use the initializer list when these situations are not present, we can always use them to improve program performance.
In our upcoming tutorial, we will discuss friend functions in C++ in detail.
=> See Here To Explore The Full C++ Tutorials list.