Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C++ Constructor Initializer List Explained

1. Introduction to Constructor Initializer List

In this article, we will learn what is Constructor Initializer List. The initializer list is helpful to load the data members of a class with a default data. It performs this initialization stuff before executing the body.

2. A Class With a Constant Data Member

Let us say a class is having a const data member in it. Where should we initialize it? We explore this with an example.

Let us say, we have a class called circle which calculates area when a radius available to it. We need a data member to hold the value of the constant Pi. In the below constructor, we attempt to initialize data members radius and the const Pi. In addition, we try to calculate the area.

When we compile the code, we get the Error C2758. Because we declare the member Pi as

const float pi;

 . But, we try to assign a value in the constructor through assignment operator.

Compiler Error C2758

Compiler Error C2758

3. Error C2758 and Constructor Initializer List

The error in the past section showed that we should initialize the const members with the Constructor Initializer List. The picture below shows how one can define the Initializer List as part of the Constructor:

Constructor Initializer List

Constructor Initializer List

The above picture shows the syntax for Constructor Initializer List and it is executed first, before executing the very first statement in the constructor’s body. Now, we should adjust our constructor so that it loads the const Pi through the initializer list. The changed constructor is below:

Note that the list is having three initialization split by a comma. We set the values to

pi, m_radius

. Also, we calculated the value for


  using this list. But, some people choose calculated values go inside the body of the constructor as it will use the already initialized member. The full example is below:

The below picture shows the output of the example:

Constructor Initializer List Example 1

Constructor Initializer List Example 1

4. Setting Different Constant Data Per Object

In the previous example, we bind the const value 3.14159 to the constant data member Pi. Sometimes, each object needs its own const value. Say, for example, you are developing a Payroll processing application, which uses a class that represents a group of employees in the form of a department. Lets us say bonus for each department varies, but we apply the same bonus to all the employees within a department. Here, the bonus is a constant which is bound to a value when we create the object. Look at the example given below:

In the example, we declare the bonus as a constant like,

const float m_bunus_percent;

. But, the const is bound to a user-supplied value of


. The below code shows the constructor initializer list:

Look at the below two statement. We have two






. But, these objects binds to different bonus percentage 10 and 5. The important thing about this is that we can not change constant objects after the binding. In our case, Welding Employees have bonus percentage of 10 and Lathe Workers have the bonus percentage of 5. This example also uses the constructor initializer list, and we require it as we have a constant member to hold the bonus percentage.

The below picture shows the output of this second example. In the output, you can see that the salary for the employee one is changed. However, we cannot change the bonus percentage.

Constructor Initializer List Example 2

Constructor Initializer List Example 2

5. Initializer List Order

The order of the constructor initializer list is not critical. Because C++ adheres to the order in which we lay the object in the class template. But, it is a best practice to keep the list to match with the data member layout of the class. In the below example, we have kept no order in the constructor initializer. This means, the order in the list does not match with the member variable layout of the class template


. The constructor initializer list is below:

In the above example, one may think the initialization order is m_glass, m_tyres and m_engine. But the order is Engine, Tyres, and Glass. This is because C++ respects the Objects layout in the class template and not the order in the Constructor. The below code shows this:

The complete example is below:


Constructor Initializer List Example 3

Constructor Initializer List Example 3

So, again, the order is based on the Layout of the class template and not the order in which the Initializer List is formed. It will be a best practice to maintain the same order in the class Layout as well as in the Initializer List. This will help a user to know about the initialization order without looking the class Layout.

6. Summary

We saw that a Constructor Initializer List is good to give a value to the const data member. Then with an example, we saw how do we have different objects of the same class type having a different constant value. Finally, in this article examined the order of Initializer List does nothing but the layout of the class will. Note, we can initialize a reference member the same way as we did for constant.

Categories: C++

Tags: ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.