Initializer List | CONSTRUCTOR | OOPS

Ad

Initializer List | CONSTRUCTOR | OOPS

Initializer List | CONSTRUCTOR | OOPS
Constructor is a special non-static member function of a class that is used to initialize objects of its class type.

In the definition of a constructor of a class, the member initializer list specifies the initializers for direct and virtual bases and non-static data members. (Not to be confused with std::initializer_list.)

A constructor must not be a coroutine. (since C++20)

The initializer list is used to directly initialize data members of a class. An initializer list starts after the constructor name and its parameters. The list begins with a colon ( : ) and is followed by the list of variables that are to be initialized – all of​ the variables are separated by a comma with their values in curly brackets.


Syntax

Constructorname(datatype value1, datatype value2):datamember(value1),datamember(value2)
{
    ...
}
Using an initialization list is almost identical to doing direct initialization (or uniform initialization in C++11).

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon.

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


1) For initialization of non-static const data members:

const data members must be initialized using Initializer List. Reason for initializing the const data member in the initializer list is because no memory is allocated separately for const data member, it is folded in the symbol table due to which we need to initialize it in the initializer list.
Also, it is a Parameterised constructor and we don’t need to call the assignment operator which means we are avoiding one extra operation.

2) For initialization of reference members:
Reference members must be initialized using Initializer List.

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

4) For initialization of base class members : 
Like point 3, the parameterized constructor of the base class can only be called using Initializer List.

5) When constructor’s parameter name is same as data member
If constructor’s parameter name is same as data member name then the data member must be initialized either using this pointer or Initializer List.

6) For Performance reasons:
It is better to initialize all class variables in Initializer List instead of assigning values inside body.

Code

The following example uses an initializer list in the default constructor to set the value for the variable value of the class.

There are several cases where the use of an initializer list is absolutely necessary, these include:

1. Initializing a reference type data member

An initialization list is used to initialize a data member of reference type. Reference types can only be initialized once.

2. Initializing const data member

const data members can be initialized only once, so they must be initialized in the initialization list.

3. Initializing member objects which do not have a default constructor

If you have a field that has no default constructor (or a parent class with no default constructor), then ​you must specify which constructor you wish to use.



EXAMPLE

#include <iostream>
using namespace std;

class Student {
        public :
       
                int age;
                const int rollNumber;
                int &x;        
                // age reference variable

               
               
                Student(int r, int age) : rollNumber(r),
                                        age(age), x(this -> age)
                {
                        //rollNumber = r;
                       
                }
}; 

int main() {
        Student s1(101, 20);
        s1.age = 20;
        //s1.rollNumber = 101;
        return 0;
}

  • To initialize the reference variable, we need to use this operator in the initialization list 
    • Student(int r, int age) : rollNumber(r), age(age), x(this -> age)

0 Response to "Initializer List | CONSTRUCTOR | OOPS"

Post a Comment

If you have any doubts, please let me know...

Ads Atas Artikel

Ads Center 1

Ads Center 2

Ads Center 3