Anonymous unions

An anonymous union is an unnamed union that does not include any declarations between the close curly that ends its body and the semicolon that ends the union definition (§ 2.6.1, p. 73). When we define an anonymous union the compiler automatically creates an unnamed object of the newly defined union type:

union {           // anonymous union    char   cval;    int    ival;    double dval;};  // defines an unnamed object, whose members we can access directlycval = 'c'; // assigns a new value to the unnamed, anonymous union objectival = 42;  // that object now holds the value 42

The members of an anonymous union are directly accessible in the scope where the anonymous union is defined.

Note

An anonymous union cannot have private ...

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.