You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The name of this type is `Rank`, by convention for a user-defined type this is in *SentenceCase*. Following the colon `:` is the *underlying type*; this **must** be a built-in integer type (`char` is also allowed) and defaults to `int` if not specified. Since we have specified `unsigned short` we can assign values from `0` to `65535` (most likely, however strictly speaking this is implementation dependent). Then, within curly braces are a list of comma-separated *enumerators*, each of which can optionally have values specified. We have set `ace = 1` instead of relying on the default value of zero for the first enumerator because it allows the internal value and representation to be the same. Subsequent enumerators take the next available value.
14
14
15
-
A variable of type `enum` (also known as *plain* enum) such as our `Rank` can be initialized from any of the enumerators listed in its definition. However, care should be taken not to assign values not in its enumeration set; this includes default-initialization:
15
+
A variable of type `enum` (also known as *plain* enum), such as `Rank` above, can be initialized from any of the enumerators listed in its definition. However, care should be taken not to assign values not in its enumeration set; this includes default-initialization if zero is not one of the enumerators:
16
16
17
17
```cpp
18
18
Rank r1{ ace }; // ok, r1 is value of enumeration constant ace (1)
@@ -23,15 +23,15 @@ auto r5 = king; // ok, r5 is of type Rank (not unsigned short)
23
23
int i = seven; // ok, implicit conversion to integral type
24
24
```
25
25
26
-
It may be surprising to discover that in most ways `ace`, `two`, `three`, `four` and so on are just "normal" integer constant values. (Indeed in some historical versions of the C language, the way to define constants was by using anonymous `enum`s; this curiosity was given the affectionate name of the "enum hack".) Thus variables of type `enum` can "borrow" enumerators from different types of `enum`s! Even worse, enumerators from different `enum` definitions in the same scope could **not** use the same name without causing a name collision.
26
+
It may be surprising to discover that in most ways `ace`, `two`, `three`, `four` and so on are just "normal" integer constant values. (Indeed in some historical versions of the C language, the only way to define constants was by using anonymous `enum`s; this curiosity was given the affectionate name of the "enum hack".) Thus variables of type `enum` can "borrow" enumerators from different types of `enum`s! Even worse, enumerators from different `enum` definitions in the same scope could **not** use the same name without causing a name collision.
27
27
28
28
To address these limitations the C++ `enum class` type was created; this type is also known as *scoped* or *strongly typed* enumeration. We can represent the suit of a card using this type:
The difference in syntax is small, we have `enum class Suit` compared to `enum Rank`. However the `none` in `Suit` does not clash with `none` in `Rank`, and related to this feature the enumerators in an `enum class` have to be qualified with the type name, as follows:
34
+
The difference in syntax is small, we have `enum class Suit` compared to `enum Rank`, although this time the underlying type is `char` and character literals are used for the enumerators. However the `none` in `Suit` does not clash with `none` in `Rank`, and related to this feature the enumerators in an `enum class` have to be qualified with the type name, as follows:
35
35
36
36
```cpp
37
37
Suit s1 = Suit::hearts; // good, types match
@@ -137,15 +137,13 @@ string_view get_color(Color c) {
137
137
switch (c) {
138
138
case Color::red:
139
139
return "red";
140
-
break;
141
140
case Color::green:
142
141
return "green";
143
-
break;
144
142
case Color::blue:
145
143
return "blue";
146
-
break;
144
+
default:
145
+
return "<nocolor>";
147
146
}
148
-
return "<nocolor>";
149
147
}
150
148
151
149
int main() {
@@ -208,15 +206,13 @@ string_view get_color(Color c) {
208
206
switch (c) {
209
207
case Color::red:
210
208
return "red";
211
-
break;
212
209
case Color::green:
213
210
return "green";
214
-
break;
215
211
case Color::blue:
216
212
return "blue";
217
-
break;
213
+
default:
214
+
return "<no color>";
218
215
}
219
-
return "<no color>";
220
216
}
221
217
222
218
int main() {
@@ -501,4 +497,4 @@ A few things to note about this program:
501
497
502
498
* Add a `static` function to calculate the diagonal distance between two `Point`s and return it as a `double`. Consider how to implement `operator/` to calculate this value, and whether this would be a suitable use of OO.
0 commit comments