Software & Finance

C++ FAQ - When to Use Initializer List for a Class


We have to initialize the reference variable at the time of definition. When we have a reference variable in a class, the only possible way to initialize them is through initializer  list of the class constructor.

class Shape



    int &m;



    Shape(int a) : m(a) { }




void TestFunction()


    int q = 10;

    Shape a(q);  // a.m will point to int q and the value would be 10