Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 32 of 442 min read8 questions

Member Initializer List

Initialize class members directly, once — and the only way to reach a base ctor.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. Written after the constructor's : and before {}; initializes members directly, once (vs default-then-assign in the body).
  2. Mandatory for const members, reference members, and members whose type has no default constructor.
  3. The only way to pass arguments to a base class constructor — even inside a default constructor.
  4. Members initialize in declaration order, NOT the order written in the list.
  5. Delegating constructor (C++11): one ctor calls another; it must be the only item in the list.
  6. Default member initializer (C++11, int a = 0;) is a fallback; the list takes precedence over it (default simply not used, not overridden). Body can assign later, but never to const/reference members.

One example that shows every point

C++
class Base {
public:
    Base(int x) {}                 // no default ctor -> base MUST be in the list
};

class Entity : public Base {
    int a = 0;                     // default member initializer (fallback)
    const int id;                  // const     -> must use the list
    int& ref;                      // reference -> must use the list
public:
    // members init in DECLARATION order (a, id, ref), not list order
    Entity(int x, int& r)
        : Base(x), a(x), id(x), ref(r)   // base first, then members, all once
    {
        a = 5;                     // body can ASSIGN a (never id/ref)
    }
    Entity(int& r) : Entity(1, r) {}     // delegating ctor: only item in the list
};
Watch out

Easily confused

initialize (list) vs assign (body)
List initializes the member directly, once. Body assigns after it already exists (default-then-overwrite for class types).
declaration order vs list order
Members init in the order they are DECLARED, not the order written in the list.
default member initializer vs MIL
Default init = fixed fallback. When a member is also in the list, the list takes precedence — the default isn't used (not overridden or reassigned). MIL is also the only way to init a base class.

Test yourself

8 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.