Skip to content

Commit 1cd9ff1

Browse files
committed
Minor changes
1 parent 3ca87fc commit 1cd9ff1

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

06-enums-and-structs.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum Rank : unsigned short { ace = 1, two, three, four, five, six, seven, eight,
1212
1313
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.
1414
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:
1616
1717
```cpp
1818
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)
2323
int i = seven; // ok, implicit conversion to integral type
2424
```
2525

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.
2727

2828
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:
2929

3030
```cpp
3131
enum class Suit : char { spades = 'S', clubs = 'C', hearts = 'H', diamonds = 'D', none = '\?' };
3232
```
3333
34-
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:
3535
3636
```cpp
3737
Suit s1 = Suit::hearts; // good, types match
@@ -137,15 +137,13 @@ string_view get_color(Color c) {
137137
switch (c) {
138138
case Color::red:
139139
return "red";
140-
break;
141140
case Color::green:
142141
return "green";
143-
break;
144142
case Color::blue:
145143
return "blue";
146-
break;
144+
default:
145+
return "<no color>";
147146
}
148-
return "<no color>";
149147
}
150148

151149
int main() {
@@ -208,15 +206,13 @@ string_view get_color(Color c) {
208206
switch (c) {
209207
case Color::red:
210208
return "red";
211-
break;
212209
case Color::green:
213210
return "green";
214-
break;
215211
case Color::blue:
216212
return "blue";
217-
break;
213+
default:
214+
return "<no color>";
218215
}
219-
return "<no color>";
220216
}
221217
222218
int main() {
@@ -501,4 +497,4 @@ A few things to note about this program:
501497

502498
* 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.
503499

504-
*All text and program code &copy;2019-2022 Richard Spencer, all rights reserved.*
500+
*All text and program code &copy;2019-2024 Richard Spencer, all rights reserved.*

0 commit comments

Comments
 (0)