Language Selector

10.1 — Constructor initialization lists

Up until now, we’ve been initializing our class member data in the constructor using the assignment operator. For example:

When the class’s constructor is executed, m_nValue, m_dValue, and m_chValue are created. Then the body of the constructor is run, where the member data variables are assigned values. This is similar to the flow of the following code in non-object-oriented C++:

While this does not exhibit good style, it is valid within the syntax of the C++ language.

So far, the classes that we have written have only included non-const or pointer member variables. However, what happens when we want to use const or reference variables as member variables? As you have learned in previous lessons, const and reference variables must be initialized on the line they are declared. Consider the following example:

This produces code similar to the following:

Consequently, assigning const or reference member variables values in the body of the constructor is not sufficient.

Initialization lists

C++ provides another way of initializing member variables that allows us to initialize member variables when they are created rather than afterwards. This is done through use of an initialization list.

In the lesson on basic addressing and variable declaration, you learned that you could assign values to variables in two ways: explicitly and implicitly:

Using an initialization list is very similar to doing implicit assignments.

Let’s take a look at our top example again. Here’s the code that does explicit assignments in the constructor body:

Now let’s write the same code using an initialization list:

The initialization list is inserted after the constructor parameters, begins with a colon (:), and then lists each variable to initialize along with the value for that variable separated by a comma. Note that we no longer need to do the explicit assignments in the constructor body, since the initialization list replaces that functionality. Also note that the initialization list does not end in a semicolon.

Here’s an example of a class that has a const member variable:

We strongly encourage you to begin using this new syntax (even if you aren’t using const or reference member variables) as initialization lists are required when doing composition and inheritance (subjects we will be covering shortly).

10.2 -- Composition
9.12 -- Shallow vs. deep copying

28 comments to 10.1 — Constructor initialization lists

  • Daniel

    Hi Alex,

    Just a question, can you use initialisation lists with arrays of members? something like this:

    Or do you have to do it like this:


    • Unfortunately, neither of those work. This actually hits on one of the shortcomings of the C++ language -- there is no way to initialize non-static array members in C++.

      However, there is a reasonable workaround. Because this is a constant array, we know the value of the members will never change -- so why even have one array per class object? It makes more sense to define m_nValue as a static member, so it will be shared by all class objects. That way they all reference the same one and you save memory. And there IS syntax to initialize static array members in C++:

      • Daniel

        Thanks, Daniel

      • Santhosh C Rao

        How do I access this variable?

        Something a;

        I was not able to compile when I used this above piece of code.
        If I make this variable as public then I was able to access.
        Can anybody tell me how to access?

        • Because m_nValue is private, you can’t access it from outside the class. The best thing to do would be to add access functions and use those:

      • Ashish

        class Something
        static const int m_nValue[3];

        const int Something::m_nValue[] = {5, 6, 7}

        ‘m_nValue’ is a private data member of the class. So how can you access it outside the class and initialize? Ans also static member data are only accessible to static member function.

        Thanks in advance

        • Martin

          Comment by Ashish
          “…static member data are only accessible to static member function.”

          However i am able to access the static member m_nValue from non-static member function GetValue(), as shown in the example below:
          //File static.h
          class Something
          static const int m_nValue[3];


          int GetValue(int nIndex)
          return m_nValue[nIndex];


          //File static.cpp
          int _tmain(int argc, _TCHAR* argv[]) //using visual studio 2005
          Something a;
          printf (“%d\n%d\n%d\n”, a.GetValue(0), a.GetValue(1), a.GetValue(2));

          return 0;

        • Rahul

          Even if it appears that m_nValue[] is accessed outside the class “Somethig”, but actually it is not.
          In the statement “const int Something::m_nValue[] = {5, 6, 7}”, we are accessing the array from inside the class. It is evident from the “Something::”.

          • cooltoad

            You can access static data from non-static member functions but not the vice versa.

            Imagine that the static data is never tied to an object and hence no this pointer.
            It is something like a global variabble for that class. That is why you can access by ClassName::static_variable.

            If the member function was static, which means it can be invoked even w/o an object being created which means it cannot access non-static data which are tied to this pointer.

  • srividya

    Its a just an awesome example

  • subhransu

    in case this slipped from your radar
    The Constructor initialize list need to initialize always for following
    1) Refrence Variables,
    2) Const Variables
    3) base class constructor(having variable number of arguments).

  • helbawi

    hi,I have question if I used this way , is my program has more safe?

  • vvidhu1988

    hi i have a question. Let say there is class A and Class B

    Class A {
    int value,
    B bMember,

    A() {
    value = 0;

    here when i create an object to Class A say

    A aMember;

    Is the constructor of class A is called and initializes ‘value’ to 0 or before calling Class A’s constructor, whether Calss B’s constructor will be called? since we also have object(bmember) of class type B.

    Please explain when class B’s constructor will be called.

    Whether in the above code i need to initialize ‘bMember’ of class type B in the Class A’s constructor?? since bMember is also a member in class A. I don’t think it is a proper implementation. Can anyone explain?

    • Nikhil Singhal


      The Ans is Very Simple……

      B constructor first called, because first declaration part is done then A’s Constructor is called, so here B’s object is created first.
      I think you got the answer
      if have any confusion run the following code in debug mode:

      class B {
      int BValue;
      BValue = 0;
      printf(“\n Class B Constructor is Called”);
      class A {
      int value;
      B bMember;

      value = 0;
      printf(“\n Class A Constructor is Called \n “);

      int main(int argc, char* argv[])
      A a1;
      return 0;

  • MrPlow442

    One question.

    Do i have to use body brackets {}?
    Can’t i just write?
    Something() : m_nValue(0), m_dValue(0.0), m_pnValue(0) ; ?

  • MK

    Hi Alex.
    thank you for your epic tutorial.

    small typo.

    “When the class’s constructor is executed, m_nValue, m_dValue, and m_chValue are created.”

    “m_chValue” should be “m_pnValue”

  • puppi

    hi alex, i have a question. can you reply it for me please?

    if we assign a const member at declare line, what then happen?

  • puppi

    yes i tried, but what are differentities between them? why it’s not recommended (this style)?

    • Bogdan

      The difference between initializing inside the class at declaration time or with initialization list is that initializing inside the class is only supported with C++11 and you need a compiler that supports C++11. This feature of the language was added in C++11. On old compilers you need to use initialization lists.

  • petewguy1234

    Good explanation! Thank you!

  • In the opening paragraph, the "m_chValue" variable name should probably be "m_pnValue".

  • Abhijeet

    Hi Alex,

    You’ve used

    to initialize the variables. There is also the option of using

    Is there any inherent benefit to using one over the other?

  • Jason

    Hi Alex,

    Greatly enjoying your tutorial and very excited to see it completely updated.  The pages you have recently worked on are truly epic.

    I may have missed it, but why does using an initialization list resolve the issues that require const and reference member fields to be initialized at the time of their declaration?

    Best wishes and thank you!

    • Alex

      > why does using an initialization list resolve the issues that require const and reference member fields to be initialized at the time of their declaration?

      Because the initialization list provides initialization values for the member variables, whereas assigning values to the members in the body of the constructor is considered an assignment (not an initialization, even though the constructor itself is called to initialize the class object). As you’re aware, const variables and references require that they be initialized.

Leave a Comment




18 + eleven =

Put C++ code inside [code][/code] tags to use the syntax highlighter