Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Member Initialization List

Tags:

c++

Please explain how to use member initialization lists. I have a class declared in a .h file and a .cpp file like this:

class Example
{
private:
    int m_top;
    const int m_size;
    ...
public:
    Example ( int size, int grow_by = 1 ) : m_size(5), m_top(-1);
    ...
    ~Example();
};

I'm initializing m_size on object creation because of const. How should I write the constructor? Should I repeat : m_size(5), m_top(-1), or I can omit this step?

Example::Example( int size, int grow_by)
{
... some code here
}

or

Example::Example( int size, int grow_by) : m_size(5), m_top(-1)
{
... some code here
}
like image 257
Yesman Avatar asked Oct 05 '11 17:10

Yesman


People also ask

What is a member initialization list?

Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

What is mean by member initialisation list of constructor in C++?

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. Following is an example that uses the initializer list to initialize x and y of Point class.

Why initialization list is used in C++?

Initialization lists allow you to choose which constructor is called and what arguments that constructor receives. If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list.

What are the advantages of initializer list?

The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.


4 Answers

Just to clarify something that came up in some of the other answers...

There is no requirement that the initialization list be in either the source (.cpp) or header (.h) file. In fact, the compiler does not distinguish between the two types of files. The important distinction is between the contructor's declaration and it's definition. The initialization list goes with the definition, not the declaration.
Usually, the declaration is in a header file and the definition is in a source file, however, this is not a requirement of the language (i.e. it will compile). It is not unusual to provide constructor definitions inline in the class declaration when the constructor is empty or short. In that case an initialization list would go inside the class declaration, which would probably be in a header file.

MyClass.h

class MyClass { public:     MyClass(int value) : m_value(value)     {} private:     int m_value; }; 
like image 76
BHS Avatar answered Sep 29 '22 16:09

BHS


This is the initialization list :

Example::Example( int size, int grow_by) : m_size(5), m_top(-1) { ... some code here } 

and it should be done only in the cpp file.

Don't you get an error when you do it like you did in the header in your example?

like image 38
BЈовић Avatar answered Sep 29 '22 15:09

BЈовић


Member Initializer list should be a part of a definition in the source file.
Write this in the .cpp file:

Example ( int size, int grow_by) : m_size(5), m_top(-1)
{

}

The header file should only have:

Example ( int size, int grow_by = 1 );

The header file only declares the constructor, the member initializer list is not a part of the declaration.

like image 35
Alok Save Avatar answered Sep 29 '22 15:09

Alok Save


Adding to others answers, the one most important thing one should remember about initialization list is that, the order of initialization is decided in the order in which the data members are declared, not the the order in which you have initialized the data members using initialization list

Consider the example (Yours):


class Example
{
private:
    int m_top;
    const int m_size;
    ...
public:
    Example ( int size, int grow_by = 1 ) : m_size(5), m_top(-1){}

                   /* Though size is initialized with a value first
                       But it is m_top variable that is assigned value -1
                       before m_size is assigned value 5 */
    ...
    ~Example(){}
};

If one is not aware of the above it can cause very serious implications.
like image 25
Arunmu Avatar answered Sep 29 '22 15:09

Arunmu