Value Ranges for Enumerations
Originally, the only valid values for an enumeration were those named in the declaration. However, C++ has expanded the list of valid values that can be assigned to an enumeration variable through the use of a type cast. Each enumeration has a range, and you can assign any integer value in the range, even if it’s not an enumerator value, by using a type cast to an enumeration variable. For example, suppose that bits
and myflag
are defined this way:
enum bits{one = 1, two = 2, four = 4, eight = 8};bits myflag;
In this case, the following is valid:
myflag = bits(6); // valid, because 6 is in bits range
Here 6
is not one of the enumerations, but it lies in the range the enumerations define.
The range is defined as ...
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.