Namara

Code daily. Without assist.

2026-08-21

init_order.cpp

#include <iostream>

struct A { A() { std::cout << "A"; } };
struct B { B() { std::cout << "B"; } };

struct C {
    B b;
    A a;
    C() : a(), b() { std::cout << "C"; }
};

int main() {
    C c;
}

What does this print?

Answer

BAC. Members are initialized in declaration order — b, then a — regardless of the order they're listed in the mem-initializer list (a(), b()). The constructor body runs last, after both members exist, printing C.