[c++] Aggregates and aggregate initialization

In C++, aggregates are a complex data type that groups multiple elements of the same or different data types into a single unit. These elements can be accessed individually or collectively. An aggregate can be a class, a struct, an array, or a union.

Aggregates in C++

In C++, any of the following types can be considered as an aggregate:

Aggregate Initialization

In C++, you can initialize a class, struct, array, or union using aggregate initialization. This is done by providing a comma-separated list of initializer values enclosed in curly braces {}.


struct Point {
    int x;
    int y;
    int z;

Point p1 = { 1, 2, 3 }; // Aggregate initialization

In the above example, the struct Point is initialized using aggregate initialization. The values 1, 2, and 3 are assigned to the members x, y, and z respectively. Aggregate initialization is a convenient way to initialize aggregates and is the preferred way of initializing aggregates in modern C++.


Not all classes in C++ can be initialized using aggregate initialization. For example, a class with private or protected data members, virtual functions, or base classes cannot be initialized using aggregate initialization.


Aggregates and aggregate initialization provide a convenient way to work with complex data types in C++. By understanding and utilizing these features effectively, developers can simplify their code and improve readability.

For more information, you can refer to the C++ reference.