What is initialization list in Constructor in C++ Programming?

Answer: Initializer list or initialization list in constructor in C++ programming is used to initialize data members of a class. Constructor of a class can have initialization list prior to constructor body i.e. list of comma separated data members to be initialized followed by a colon ( notice the constructor initialization list order) . for example, data member variable a and b has been initialize using constructor initialization list.

class Diamond{

private:
	const int a;//constant
	int &b; //reference
	
public:
	Diamond( int i, int j, int k):a(i),b(j) //initialization list.
	{
		//Construcotor body		
	} 
};

We can use assignment of variable instead of initialization list but sometimes, it is mandatory to use initialization list to initialize the variable. For example, constant member variable or reference variables must be initialized and we cannot use assignment in constructor body for this variable.

If we assign value to these variables in constructor body compiler will flash an error i.e.

error : classname::variable’ : must be initialized in constructor base/member initializer list      

So, only option is initialization list for constant and reference variables, in below example, a and b are constant and reference variable respectively, that must be used in initialization list for initialization. We have used a normal variable int c that can be used in either in initialization list or can be assigned by a value in constructor body.

class Diamond{

private:
	const int a;//constant
	int &b; //reference
	int c;

public:
	Diamond( int i, int j, int k):a(i),b(j) //initialization list.
	{
		//Construcotor body
		this->c=k; //assignment		
	} 
};

NOTES:

  • In fact, there is a difference between initialization and assignment of a variable in C++ programming. if we assign a value to a variable at the time of declaration is know as initialization, but, if we assign a value at a later point of time is known as assignment.
    But, generally, we use term initialization, when we first assign a value to a variable in constructor body as a default value.
  • We can use both, either c++  initialization list or assignment operator in constructor body. But in constructor body, one more extra process is happening that is assignment operator is getting called for every variables i.e. constructor is crating variables and then assignment operator is getting called to assign the variable.
    So, it, sounds performance difference, but, as a practice, we don’t see significant difference. However, if every bit of performance is required then use initialization list.But, in real industries, we prefer initializer list if number of variables are less or they are constant or reference variable. If we have large number of variables in a class then we prefer to use assignment operator to make code readable. Just assume, if we have 50+ variables in a class which one would be easy to maintain.For example, for huge numbers of variable “code comments” help identify variables quickly for modification at a later point of time e.g.
class Diamond{

private:
	const int a;//constant
	int &b; //reference
	int temprature;
	float data;
	float precision;
	int simcount;
	//40+ more variables


public:
	Diamond( int i, int j, int k):a(i),b(j) //initialization list.
	{
		//Construcotor body
		this->c=k; //assignment
		//GROUPS
		
			// 10 variable

		//CALCULATIONS

			//30 vrialbes

		//STATUS

			//40 variables

	} 
}; 

  • Don’t get confused with initializer list in C++ 11.

Related Posts