What are Aggregates?

What are Aggregates?

The term “aggregate” is used in the ANSI standard to denote an array or a class with no constructors, no private or protected data members, no base classes, and no virtual functions. In other words, an aggregate is a POD (plain old data object or an array thereof, see also What are Plain Old Data (POD) Objects?.

Aggregates differ from objects and arrays of object in several ways. In particular, you can initialize every aggregate of every type and size by the ‘={0};’ initializer list. This initializer list guarantees that the entire aggregate is zero-initialized:

 struct Employee{  int ID;  int rank  char name[12];  int salary;};  // using the  '={0}' initializer to initialize aggregatesEmployee emp = {0}; //all emp members are zero-initializedEmployee emp_arr[10] = {0}; //all array elements are zero-initialized

Note that non-aggregate objects and arrays cannot be initialized this way; instead, they are initialized by a constructor.


Share the Post: