Scoped Enumerations (C++11)
Traditional enumerations have some problems. One is that enumerators from two different enum
definitions can conflict. Suppose you were working on a project involving eggs and T-shirts. You might try something like this:
enum egg {Small, Medium, Large, Jumbo};enum t_shirt {Small, Medium, Large, Xlarge};
This won’t fly because the egg Small
and the t_shirt Small
would both be in the same scope, and the names conflict. C++11 provides a new form of enumeration that avoids this problem by having class scope for its enumerators. The declarations for this form look like this:
enum class egg {Small, Medium, Large, Jumbo};enum class t_shirt {Small, Medium, Large, Xlarge};
Alternatively, you can use the keyword struct
instead ...
Get C++ Primer Plus 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.