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

c++, constructor, ctor-initializer, initialization, member-initialization

Solution

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.

Problem

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 */ } ```

Original source