Skip to content

Commit 0e88cd9

Browse files
committed
Updated to Chapter 6, Section 1
1 parent ebbda11 commit 0e88cd9

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
11
\section{枚举常量}
2+
定义一个枚举常量的基本格式如下:\footnote{本节只介绍无作用域枚举,关于有作用域枚举,请见精讲篇。}
3+
\begin{lstlisting}
4+
enum <枚举名> {<枚举项>=<整型常量表达式>, <枚举项>=<整型常量表达式>, ...};
5+
\end{lstlisting}\par
6+
枚举名不是必须的。如果不使用枚举名,那么这些枚举项的类型就是匿名枚举,这样做相当于定义了一些整型常量。如果我们规定了名字,那它们就是具名枚举,有丰富的作用。\par
7+
枚举常量都是基于整型的,它的枚举项可以隐式类型转换为整型来输出。
8+
\begin{lstlisting}
9+
enum {A=1, B=3, C=5}; //不具名枚举
10+
cout << B; //输出3
11+
\end{lstlisting}
12+
我们可以省略其中的部分枚举项的值,这时枚举项的值是基于前一个值递增的。
13+
\begin{lstlisting}
14+
enum {A=-2, B=4, C, D}; //C为递增为5,D递增为6
15+
enum {E, F, G}; //第一项E不设值则为0,后面F,G分别为1和2
16+
\end{lstlisting}
17+
如果我们需要一次性定义大量整型常量,那么除了 \lstinline@const@ 及 \lstinline@constexpr@ 语法之外,我们还可以选择 \lstinline@enum@。\par
18+
如果这个枚举有枚举名,那么我们还可以把它作为一个类型来用!具名枚举类型的优点是:
19+
\begin{itemize}
20+
\item 它的值可以用标识符来表示,而不是用单纯的字面量——我们提过,常量表达式比字面量的一大好处在于方便在源代码层面做统一修改。
21+
\item 具名枚举类型可以隐式转换为整型常量,所以我们使用起来很方便。
22+
\item 整型常量不能隐式转换为具名枚举类型,而且具名枚举类型数据的取值范围有很大限制,我们可以很防止这类数据取一些不允许的值。
23+
\end{itemize}
24+
举个例子,想必读者就可以理解了。关于服装的尺码,不同国家/地区都有各自的规定。以男士西装为例,欧洲/俄罗斯、英国/美国、日本、韩国都有自己的尺寸对照表。我们可以用枚举常量来表示各个尺寸。
25+
\begin{lstlisting}
26+
enum SuitsSize {
27+
XXS=40, XS=42, S_1=44, S_2=46, M_1=48, M_2=50,
28+
L_1=52, L_2=54, XL=56, XXL=58, XXXL=60
29+
}; //这是欧洲/俄罗斯标准
30+
\end{lstlisting}
31+
接下来我们可以定义一些 \lstinline@SuitsSize@ 类型的变量,比如说就定义成对应的人名吧。
32+
\begin{lstlisting}
33+
SuitsSize Erik {XXL}; //Erik的值就是XXL,不是58,只是它可以隐式转换成58
34+
SuitsSize Jacques {XS}; //Jacques的值就是XS
35+
SuitsSize Carlos {M_1}; //M尺码有两个,为了区分而分别写成M_1和M_2
36+
\end{lstlisting}\par
37+
当我们想要知道枚举项的具体值时,我们可以直接以 \lstinline@int@ 类型输出。
38+
\begin{lstlisting}
39+
cout << static_cast<int>(XL); //或者直接cout<<XL,会隐式类型转换
40+
\end{lstlisting}\par
41+
如果某个人的尺码发生了改变,我们可以直接赋值来改变,非常方便。
42+
\begin{lstlisting}
43+
Erik = XXXL; //胖了
44+
\end{lstlisting}
45+
但是我们赋值也不能乱来;我们只能赋那些已经规定好了的项。
46+
\begin{lstlisting}
47+
Eirk = XXXXXXXL; //错误!
48+
\end{lstlisting}
49+
既然 \lstinline@SuitsSize@ 也是一个类型,那么我们当然还可以定义这个类型的常量数据。
50+
\begin{lstlisting}
51+
const SuitsSize Giovanni {XXXL}; //Giovanni的值一经定义,就不可改变
52+
\end{lstlisting}\par
53+
总而言之,我们可以把具名枚举的各项看作是整型数据,但是我们应该先把它当成一种特殊的自定义类型,它的值就是标识符本身,只不过可以转换为整型而已。作为类比,我们可以想一下 \lstinline@bool@ 类型,它的值就是标识符 \lstinline@true@ 或 \lstinline@false@ 本身,只是它可以隐式转换成整型而已。\par

main.log

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex 2023.12.3) 3 JAN 2024 17:24
1+
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex 2023.12.3) 3 JAN 2024 20:32
22
entering extended mode
33
\write18 enabled.
44
file:line:error style messages enabled.
@@ -1211,11 +1211,11 @@ Missing character: There is no � (U+FFFD) in font [SourceCodePro-Regular.otf]/
12111211
Missing character: There is no � (U+FFFD) in font [SourceCodePro-Regular.otf]/OT:script=latn;language=dflt;!
12121212
File: ./generalized_parts/../images/generalized_parts/06_0_1_string_to_int_or_char_300.png Graphic file (type bmp)
12131213
<./generalized_parts/../images/generalized_parts/06_0_1_string_to_int_or_char_300.png>
1214-
(./generalized_parts/06_custom_types_and_their_use/01_enum.tex) (./generalized_parts/06_custom_types_and_their_use/02_struct.tex) (./generalized_parts/06_custom_types_and_their_use/03_union.tex) (./generalized_parts/06_custom_types_and_their_use/04_introduction_to_class.tex))) (././intensive_part.tex [125
1214+
(./generalized_parts/06_custom_types_and_their_use/01_enum.tex [125
12151215

1216-
] [126
1216+
]) (./generalized_parts/06_custom_types_and_their_use/02_struct.tex) (./generalized_parts/06_custom_types_and_their_use/03_union.tex) (./generalized_parts/06_custom_types_and_their_use/04_introduction_to_class.tex))) (././intensive_part.tex [126] [127
12171217

1218-
] [127] [128]) (./other_parts/appendix.tex (./other_parts/appendices/A.tex
1218+
] [128]) (./other_parts/appendix.tex (./other_parts/appendices/A.tex
12191219
附录 A
12201220
LaTeX Font Info: Font shape `TU/SourceCodePro(0)/m/n' will be
12211221
(Font) scaled to size 8.33344pt on input line 2.
@@ -1265,10 +1265,10 @@ Package rerunfilecheck Info: File `main.out' has not changed.
12651265
(rerunfilecheck) Checksum: D9E7CE1EFF17417B60F21EE2E1C28EE7;4444.
12661266
)
12671267
Here is how much of TeX's memory you used:
1268-
24960 strings out of 474924
1269-
609513 string characters out of 5765037
1268+
25000 strings out of 474924
1269+
609889 string characters out of 5765037
12701270
1995189 words of memory out of 5000000
1271-
46265 multiletter control sequences out of 15000+600000
1271+
46305 multiletter control sequences out of 15000+600000
12721272
571813 words of font info for 164 fonts, out of 8000000 for 9000
12731273
1348 hyphenation exceptions out of 8191
12741274
93i,17n,111p,1231b,1834s stack positions out of 10000i,1000n,20000p,200000b,200000s

main.pdf

8.07 KB
Binary file not shown.

main.synctex.gz

14.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)