Yesman
Yesman

Reputation: 427

Is a member initializer list part of the declaration or the definition of a constructor?

Please explain how to use member initializer 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)
{
    /* ... */
}

or

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

Upvotes: 30

Views: 74720

Answers (6)

minus
minus

Reputation: 706

You cannot have an initializer list both in the header and the source file. The list should be in whichever file defines your constructor. Also you must include a body when defining the constructor, even if it's empty.

On a side note, you should only specify the default arguments in the function prototype, not in the definition.

Upvotes: 1

Arunmu
Arunmu

Reputation: 6901

Adding to others answers, the one most important thing one should remember about the initializer 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 your example:

class Example
{
private:
    int m_top;
    const int m_size;
    /* ... */
public:
    Example(int size, int grow_by = 1) : m_size(5), m_top(-1) {}
    /* m_size appears to be initialized with a value first,
     * but m_top is initialized to the value -1 before
     * m_size is initialized to 5. */
    
    /* ... */
    ~Example(){}
};

If one is not aware of the above, it has very serious implications.

Upvotes: 9

Alok Save
Alok Save

Reputation: 206636

The member initializer list should be a part of the constructor 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.

Upvotes: 15

BЈовић
BЈовић

Reputation: 64283

This is the initializer list:

Example::Example( int size, int grow_by) : m_size(5), m_top(-1)
{
    /* ... */
}

and it should be done only in the .cpp file.

If you did it in the header like in your example, you would get an error.

Upvotes: 26

BHS
BHS

Reputation: 1081

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

There is no requirement that the initializer 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 initializer 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 initializer 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;
};

Upvotes: 65

emsr
emsr

Reputation: 16373

In C++11 you can use non-static data member initialization. This is especially useful if you have several constructors that need a common value for a member variable.

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

...

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

You can override the value in a constructor if you need to.

Upvotes: 1

Related Questions