C++20 sections · 1024 units
Open in Course

Member Initializer Lists

Efficient initialization

Use a colon after the constructor signature followed by member initializations: Point(int a, int b) : x(a), y(b) {}. This initializes members directly instead of assigning after creation.

For some types like constants or references, you must use initializer lists because you can't assign to them later. It also avoids double initialization for class-type members. Members initialize in declaration order, not the order you list them.

Match the initializer list order to declaration order to avoid confusion.