Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

Initializer list ==>

============================
It is one of the important concept in c++. It is the list of variable that are used
to initialize the data member of a class. It is written with constructor followed
by color(:) and each variable of initializer list is seperated comma(,). following
is example -

example - 1
#include <iostream>
using namespace std;

class Points{
int x;
int y;

public:
Points(int i, int j) : x(i), y(j){}

int getX() { return this->x;}


int getY() { return this->y;}
};

int main(){
Points point(11, 9);
cout << "value of x : " << point.getX() << endl << "value of y : " <<
point.getY() << endl;
return 0;
}
output ->
value of x : 11
value of y : 9
--------------------------

in this example, data member of class Points is initialized with the value of i and
j.

example - 2
class Points{
int x;
int y;

public:
Points(int i, int j) {
x = i;
y = j;
}

int getX() { return this->x; }


int getY() { return this->y; }
};

int this example, we assign the value of x and y inside parameterize constructor.
We will get the same result as above example. But, this is not the example of
initilizer list. Here, actually inside constructor we assign the value of data
member. because, According to this example, first variable is created and assign
the value with 0 and then compiler executes the code inside of constructor and
assign the new value of data member x and y.

But, when we use initializer list then variable is created and assign the value as
initializer list's variable.

there are two way to use initializer list ->


.................................................
(1) using () brackets
(2) using {} brackets

Both bracket work as same but, there is minor difference between both. When we use
() bracket then it perform narrow-casting without warning.
example - 3
#include <iostream>
using namespace std;

class Points{
char x;
int y;

public:
Points(int i, int j) : x(i), y(j) {}

int getX() { return this->x; }


int getY() { return this->y;}
};

int main(){
Points point(300, 356);
cout << "value of x : " << point.getX() << endl;
cout << "value of y : " << point.getY() << endl;
return 0;
}
in this example, data type of x is char and pass as parameter to int value then
here ,narrow-casting is perform without warning.

If we use {} brackets then it also perform narrow-casting but, it show warnnig


message.
example - 4
#include <iostream>
using namespace std;

class Student{
char roll;

public:
Student(int r) : roll{r} {}

void print(){
cout << "roll : " << (int)this->roll << endl;
}
};

int main(){
Student stud(301);
stud.print();
return 0;
}
this example also show the ouput with narrow-casting. But, {} brackets show warning
message related to narrow-casting. therefore, It is prefered to use. Because, It
aware to programmer.
following are some important situation where initializer list has to use
===========================================================================
(1) to initialize non-static data member of a class =>
#include <iostream>
using namespace std;

class Student{
public:
const string college;

Student(){
this->college = "Gaya college, Gaya";
}

void print(){
cout << "College name : " << this->college << endl;
}
};
in this example, we assign the const data mamber inside the constructor so, we will
get compile time error. because, C++ does not allow to re-assign the const data
member. so at this point we need to use initializer list. as show in the following
example -
#include <iostream>
using namespace std;

class Student{
public:
const string college;

// Student(){
// this->college = "Gaya college, Gaya";
// }

Student(string college) : college{college} {}

void print(){
cout << "College name : " << this->college << endl;
}
};

int main(){
Student student("Gaya college, Gaya");
student.print();
return 0;
}
this example works fine and show expected output.

(2) to initialize reference member =>


#include <iostream>
using namespace std;

class Student{
int &_roll;

public:
Student(int roll) : _roll{roll} {}

void print(){
cout << "roll number : " << this->_roll << endl;
}
};

int main(){
Student std(101);
std.print();
return 0;
}
output ===>
roll number : 101
Without initializer list we cann't initialize the reference data member.

(3) to initialize the member object which has no default constructor =>
#include <iostream>
using namespace std;

class One{
int _x;

public:
One(int x) : _x{x} {}

int getValue(){
return this->_x;
}
};

class Two{
One a;

public:
Two(One x) : a{x} {}

void print(){
cout << "value of _x is : " << a.getValue() << endl;
}
};

int main(){
Two two(11);
two.print();
return 0;
}

in this example, if we modify class One as follow -


class One{
public:
int _x;

One(int x){
_x = x;
}
};

and compile above program then it show error. Because, without initilizer list
object member of a class does not initialize if default constructor is not
available. if default constructor is available then there is not need of
initializer list.
example -
#include <iostream>
using namespace std;

class One{
int _x;

public:
One(){}
// One(int x) : _x{x} {}

int getValue(){
return this->_x;
}
};

class Two{
One a;

public:
// Two(One x) : a{x} {}

Two(One x){
a = x;
}
void print(){
cout << "value of _x is : " << a.getValue() << endl;
}
};

int main(){
// Two two(11);
One o;
Two two(o);
two.print();
return 0;
}
this example, compile and works fine.

(4) to initialize base class data member from child class =>
without initializer list we can not initialize the data member of base class from
child class. example -
#include <iostream>
using namespace std;

class Base{
int _x;

public:
Base(int x){
_x = x;
}

int getValue() { return this->_x;}


};

class Child : public Base{


public:
int _y;
Child(int x, int y) : Base{x}, _y{y} { }
};

int main(){
Child child(12, 11);
cout << "value of _x : " << child.getValue() << endl;
cout << "value of _y : " << child._y << endl;
return 0;
}
in this example, data member of base class is private. so, initializer list is used
to initialize that data member. if data member of base class is public then there
is not need of initializer list to initialize data mamber.

(5) when constructor's parameter name is same as data member =>


#include <iostream>
using namespace std;

class Student{
public:
int roll;
Student(int roll){
roll = roll;
}

void print(){
cout << "roll : " << roll << endl;
}
};

int main(){
Student std(12);
std.print();
return 0;
}
this program compile and run successfully, but it show unexpected value. Because,
constructor's parameter name is same as data member. to resolve this problem
initializer list is used. As shown in the following example -
#include <iostream>
using namespace std;

class Student{
public:
int roll;
// Student(int roll){
// roll = roll;
// }

Student(int roll) : roll{roll} {}

void print(){
cout << "roll : " << roll << endl;
}
};

int main(){
Student std(12);
std.print();
return 0;
}
(6) for better performance => It is better to use initializer list to initialize
the variables instead of assign value. Because, initialize r list work fast.

You might also like