June 2025
Intermediate to advanced
1093 pages
33h 24m
English
You are already familiar with the simple struct Person:
struct Person { string name_; int age_; string city_; };
You also know that you can initialize the aggregate Person with its three elements in four correct ways:[ 18 ]
Person p1 { name, age, city };Person p2 { name, age };Person p3 { name };Person p4 { };
Now, if we add the designated initializers introduced in C++20, there are seven more possibilities:
Person p1 { .name_=name, .age_=age, .city_=city };Person p2 { .name_=name, .city_=city }; // and 2 morePerson p3 { .age_=age }; // and 2 more
All these variants are valid and initialize all fields. The ones not listed are value-initialized, meaning they are filled with a meaningful, usually null-like ...
Read now
Unlock full access