C++ 语言枚举类 (enum class)
1. enum class
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
enum Season
{
Summer,
Spring,
Winter,
Autumn
};
enum Color {
Blue,
Pink,
Green
};
int main()
{
Season s = Summer;
Color c = Blue;
if (s == c) {
std::cout << "Equal" << std::endl;
}
else {
std::cout << "Not Equal" << std::endl;
}
return 0;
}
Here, we want to check whether Summer
is equal to Blue
which is obviously not true
since one of these is Season
and the other a Color
. In the above example, we got the output true
because Summer
and Blue
got converted into integers and then those integers got compared.
在这里,我们要检查 Summer
是否等于 Blue
,这显然不是 true
,因为其中一个是 Season
,另一个是 Color
。 在上面的例子中,我们得到了输出 true
,因为 Summer
和 Blue
被转换成整数,然后这些整数被比较。
Equal
请按任意键继续. . .
The values of both Summer
and Blue
is 0 and thus the values of s
and c
became 0. So, the condition (s == c
) became true
and Equal
got printed on the screen. This is not the desired output because we cannot compare a Season
and a Color
. Since Season
and Color
belong to different enumerations, therefore these should not be compared.Summer
和 Blue
的值为 0,因此 s
和 c
的值变为 0。因此,条件 (s == c
) 变为 true
,并且打印了 Equal
屏幕上。这不是所需的输出,因为我们无法比较 Season
and Color
。由于 Season
and Color
属于不同的枚举,因此不应将它们进行比较。
Here comes in enum class
which limits the scope of the enumerators within their enums. So, now any enumerator will be known by its enum thus limiting its scope within the enum to which it belongs. This is the reason enum class
is also called scoped enumeration
.
这里有 enum class
,它限制了枚举中枚举数据的范围。因此,现在任何枚举数据都将通过其枚举知道,从而将其范围限制在它所属的枚举内。这就是枚举类也称为作用域枚举的原因。
To make an enum class
, we simply have to add the keyword class
after the keyword enum
.
要创建一个 enum class
,我们只需在关键字 enum
之后添加关键字 class
。
enum class Season
{
Summer,
Spring,
Winter,
Autumn
};
We made our enum Season
an enum class
by adding the keyword class
after the keyword enum
. Now let’s see how to access any element (enumerator) of this enum class
.
通过在关键字 enum
之后添加关键字 class
,我们使我们的 enum Season
成为 enum class
。现在让我们看看如何访问这个 enum class
的任何元素。
Season::Summer
The ::
symbol simply means belong to. Here, Summer
belongs to the enum class Season
and thus cannot be directly accessed anywhere.::
符号仅表示属于。在这里,Summer
属于 enum class Season
,因此不能在任何地方直接访问。
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
enum class Season
{
Summer,
Spring,
Winter,
Autumn
};
enum class Color {
Blue,
Pink,
Green
};
int main()
{
Season s = Season::Summer;
Color c = Color::Blue;
if (s == c) {
std::cout << "Equal" << std::endl;
}
else {
std::cout << "Not Equal" << std::endl;
}
return 0;
}
1>------ Build started: Project: hash_table, Configuration: Debug Win32 ------
1>
1>d:\visual_studio_workspace\hash_table\hash_table\(30): error C2676: binary '==': 'Season' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
In this example, we cannot directly access the enumerators (. we cannot directly access it by writing Summer
or Blue
as we did in the first example ). We have to write the enum class
name followed by ::
before the enumerator name while accessing it because now the enumerator will be known by its enum class
.
在这个例子中,我们不能直接访问枚举数据 (即我们不能像在第一个例子中那样通过写 Summer
或 Blue
来直接访问它)。 我们必须在访问枚举器名称之前写入 enum class
名称,后跟 ::
,因为现在枚举数据将通过它的 enum class
识别。
As in the above example, we accessed the Summer
enumerator as Season::Summer
thus telling the compiler that we are accessing Summer
which is a Season
. Similarly, by writing Color::Blue
, we are telling the compiler that we are accessing Blue
which is a Color
. When we wrote the condition (s == c
), we are comparing a Season
and a Color
thus getting an error.
在上面的例子中,我们对 Summer
枚举数据通过 Season::Summer
访问,从而告诉编译器我们正在访问 Summer
,它是一个 Season
。类似地,通过编写 Color::Blue
,我们告诉编译器我们正在访问 Blue
,它是一个Color
。当我们编写条件 (s == c
) 时,我们正在比较一个 Season
和一个 Color
,从而得到一个错误。
We can compare the enumerators which belong to the same enum class
.
我们可以比较属于同一个 enum class
的枚举数据。
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
enum class Color {
Blue,
Pink,
Green
};
int main()
{
Color c = Color::Blue;
if (c == Color::Blue) {
std::cout << "Your color is Blue" << std::endl;
}
else if (c == Color::Pink) {
std::cout << "Your color is Pink" << std::endl;
}
else {
std::cout << "Your color is Green" << std::endl;
}
return 0;
}
Your color is Blue
请按任意键继续. . .
Note that enumerators are not converted to integers in the case of enum class
. By writing Color c = Color::Blue;
, we are assigning the color Blue
of enum class Color
to the variable c
. So the condition (c == Color::Blue
) became true and "Your color is Blue"
got printed. In this condition, we compared c
and Color::Blue
, both of which belong to the enum class Color
.
请注意,在 enum class
的情况下,枚举数据不会转换为整数。通过编写 Color c = Color::Blue;
,我们将 enum class Color
的 color Blue
分配给变量 c
。 所以条件 (c == Color::Blue
) 变为真,并且打印出 "Your color is Blue"
。在这种情况下,我们比较了 c
和 Color::Blue
,它们都属于 enum class Color
。
2. Example
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
enum class RetVal
{
SUCCESS = 0,
FAILURE = 1
};
enum class DebugLevelType
{
NONE = 0,
PARTIAL = 1,
FULL = 2
};
enum class CompilerType
{
ONLINE = 0,
OFFLINE = 1
};
enum class Color {
Blue,
Pink,
Green
};
int main()
{
Color c = Color::Blue;
if (c == Color::Blue) {
std::cout << "Your color is Blue" << std::endl;
}
else if (c == Color::Pink) {
std::cout << "Your color is Pink" << std::endl;
}
else {
std::cout << "Your color is Green" << std::endl;
}
return 0;
}
Your color is Blue
请按任意键继续. . .
References
/cpp-enum-class/