C++11—Scoped enums


In Fig. 6.10, we introduced enums. One problem with enums (also called unscoped enums) is that multiple enums may contain the same identifiers. Using such enums in the same program can lead to naming collisions and logic errors. To eliminate these problems, C++11 introduces so-called scoped enums, which are declared with the keywords enum class (or the synonym enum struct). For example, we can define the Status enum of Fig. 6.10 as:

enum class Status { CONTINUE, WON, LOST };

To reference a scoped enum constant, you must qualify the constant with the scoped enum’s type name (Status) and the scope-resolution operator (::), ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.