What are initializer lists in C++?

What are initializer lists in C++? 

1. Initializer list is used to initialize data members of a class.

2. The list of members to be initialized is indicated with constructor as a comma separated list followed by a colon.


Syntax of initializer list in C++


constructorName (dataType val1, dataType val2):dataMember(val1),dataMember(val2)
{
//...
}


For Example:- initializer list in c++

#include<iostream>
using namespace std;

Class DummyClass
{
private:
int num1, num2;

public:
//default constructor
DummyClass(): num1(51), num2(54) //initializer list
{
cout << "The Value is: " << num1 << num2;
}
};

int main()
{
DummyClass D1;
return 0;
}


Why do we use initializer list in C++?

There are some situations where initialization of data members inside constructors does not work and hence initialization list came into picture. Initializer lists are used:

1. For initialization of non-static const data members.
2. For initialization of reference members.
3. For initialization of member objects which do not have default constructor.
4. For initialization of base class members.


For initialization of non-static const data members:

const variable must be initialized during its creation or declaration but when we declare const variable inside a class as a member variable, we can't initialize it.

You can't initialize member variable during its declaration inside a class. You can't also initialize const variable inside a constructor. That's why we need initializer list to initialize const variable.


For Example:- initializer list for const variable in c++
#include<iostream>
using namespace std;

class DummyClass
{
const int num;

public:
DummyClass(int n1):num(n1) { } //Initializer list must be used

int getNUM()
{
return num;
}
};

int main()
{
DummyClass D1(15);
cout << D1.getNUM();
return 0;
}


For initialization of reference members:

reference variable must be initialized during its declaration but when we declare reference variable inside a class as a member variable, we can't initialize it.

You can't initialize member variable during its declaration inside a class. You can't also initialize reference variable inside a constructor. That's why we need initializer list to initialize reference variable.


For Example:- initializer list for reference variable in c++
#include<iostream>
using namespace std;

class DummyClass
{
const int &ref_var;

public:
DummyClass(int &ref):ref_var(ref) { } //Initializer list must be used

int getNUM()
{
return ref_var;
}
};

int main()
{
int num1 = 11;
DummyClass D1(num1);
cout << D1.getNUM() << endl;

num1 = 21;
cout << D1.getNUM() << endl;
return 0;
}


For initialization of member objects which do not have default constructor:

#include<iostream>
using namespace std;

class Dummy1
{
int i;

public:
Dummy1(int );
};

Dummy1 :: Dummy1(int arg)
{
i = arg;
cout << "Dummy1's Constructor called: Value of i: " << i << endl;
}

// Class Dummy2 contains object of class Dummy1
class Dummy2
{
Dummy1 a;

public:
Dummy2(int );
};

Dummy2 :: Dummy2(int x):a(x) //Initializer list must be used
{
cout << "Dummy2's Constructor called";
}

int main()
{
Dummy2 obj(12);
return 0;
}

If class Dummy1 had both default and parameterized constructors, then Initializer List is not must if we want to initialize “a” using default constructor, but it is must to initialize “a” using parameterized constructor.


For initialization of base class members:

Like point 3, the parameterized constructor of the base class can only be called using Initializer List.
#include<iostream>
using namespace std;

class Dummy1
{
int i;

public:
Dummy1(int );
};

Dummy1 :: Dummy1(int arg)
{
i = arg;
cout << "Dummy1's Constructor called: Value of i: " << i << endl;
}

// Class Dummy2 is derived from class Dummy1
class Dummy2 : Dummy1
{
public:
Dummy2(int );
};

Dummy2 :: Dummy2(int x):Dummy1(x) //Initializer list must be used
{
cout << "Dummy2's Constructor called";
}

int main()
{
Dummy2 obj(21);
return 0;
}

Reference >> GeeksforGeeks



<< Previous                                                                                      Next >>
Previous
Next Post »