Initializer List in C++

Initializer list is used to initialize data members. The syntax begins with a colon(:) and then each variable along with its value separated by a comma. The initializer list does not end in a semicolon.

Syntax:

Constructorname(datatype value1, dat type value2):datamember(value1),datamember(value2)
{
...
}

Example:

#include<iostream>
using namespace std;
class Base
{
private:
int value;
public:
Base( int value) : value (value)
{
cout<<"Value is "<<value;
}
};
int main()
{
Base il(10);
return 0;
}

Output:

Value is 10

The above code is just an example for syntax of Initializer list. In the above code, value can also be easily initialized inside the constructor.


Uses of Initializer List

There are situations where initialization of data members inside constructor doesn't work and Initializer List must be used. Following are such cases:

When no base class default constructor is present

The base class constructor is called first, followed by child class constructor. Therefore, Base_ class constructor is called before InitilizerList_ constructor. The below program throws compilation error: "No default constructor exists for class Base_".

#include<iostream>
using namespace std;
class Base_
{
public:
Base_( int x )
{
cout<<"Base Class Constructor value is"<<x<<endl;
}
};
class InitilizerList_ : public Base_
{
public:
InitilizerList_()
{
Base_ b(10);
cout << "InitilizerList_'s constructor" << endl;
}
};
int main()
{
InitilizerList_ il;
return 0;
}

The above example can rewritten using initializer list.

#include<iostream>
using namespace std;
class Base_
{
public:
Base_( int x )
{
cout<<"Base Class Constructor value is "<<x<<endl;
}
};
class InitilizerList_ : public Base_
{
public:
InitilizerList_(): Base_( 10 )
{
cout << "InitilizerList_'s constructor" << endl;
}
};
int main()
{
InitilizerList_ il;
return 0;
}

Output:

Base Class Constructor value is 10
InitilizerList_'s constructor

When reference type is used

If you have a data member as reference type, you must initialize it in the initialization list. References are immutable they can be initialized only once.

#include<iostream>
using namespace std;
class Base
{
private:
int &ref;
public:
Base( int &ref ) :ref(ref)
{
cout<<"Value is"<<ref;
}
};
int main()
{
int ref=10;
Base il(ref);
return 0;
}

Output:

Value is 10

For initializing const data member

const data memebers can be initialized only once, so it must be initialized in the initialization list.

#include<iostream>
using namespace std;
class Base
{
private:
const int c_var;
public:
Base( int c_var) : c_var (c_var)
{
cout<<"Value is"<< c_var;
}
};
int main()
{
Base il(10);
}

Output:

Value is 10

When data member and parameter have same name

#include<iostream>
using namespace std;
class Base
{
private:
int value;
public:
Base( int value) : value (value)
{
cout<<"Value is"<<value;
}
};
int main()
{
Base il(10);
return 0;
}

Output:

Value is 10

For improving performance

If you are assigning the values inside the body of the constructor, then a temporary object would be created and given to the assignment operator. The temporary object will be destroyed at the end of the assignment statement. Creation of temporary object can be avoided by using initializer list.