7.5.5. Aggregate Classes

Image

An aggregate class gives users direct access to its members and has special initialization syntax. A class is an aggregate if

• All of its data members are public

• It does not define any constructors

• It has no in-class initializers (§ 2.6.1, p. 73)

• It has no base classes or virtual functions, which are class-related features that we’ll cover in Chapter 15

For example, the following class is an aggregate:

struct Data {    int ival;    string s;};

We can initialize the data members of an aggregate class by providing a braced list of member initializers:

// val1.ival = 0; val1.s = string("Anna")Data val1 = { 0, "Anna" ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.