Enumerators

The names of the enumerators in a scoped enumeration follow normal scoping rules and are inaccessible outside the scope of the enumeration. The enumerator names in an unscoped enumeration are placed into the same scope as the enumeration itself:

enum color {red, yellow, green};      // unscoped enumerationenum stoplight {red, yellow, green};  // error: redefines enumeratorsenum class peppers {red, yellow, green}; // ok: enumerators are hiddencolor eyes = green; // ok: enumerators are in scope for an unscoped enumerationpeppers p = green;  // error: enumerators from peppers are not in scope                    //     color::green is in scope but has the wrong typecolor hair = color::red;   // ok: we can explicitly access the enumerators ...

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.