A special syntax is used for initializing subelements of objects with constructors. Constructor initializers for structure and class members are specified by a colon and a comma-separated list that follows the constructor parameter list and that precedes the code body. A constructor initializer is a data member identifier followed by a parenthesized expression. Using this syntax, the counter constructor can be recoded as
// Default constructor for counter
inline counter::counter(int i = 0) :
value(i % 100) { }
The member variable value is initialized by the expression i % 100. The constructor definition has a compound statement that is empty. Notice that initialization replaces assignment. The individual members must be initializable as
member-name (expression list)
It is not always possible to assign values to members in the body of the constructor. An initializer list is required when a nonstatic member is either a const or a reference type.