IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ 语言中的枚举类 (enum class) -> 正文阅读

[C++知识库]C++ 语言中的枚举类 (enum class)

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,因为 SummerBlue 被转换成整数,然后这些整数被比较。

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.
SummerBlue 的值为 0,因此 sc 的值变为 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>  yongqiang.cpp
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(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 (i.e. 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.
在这个例子中,我们不能直接访问枚举数据 (即我们不能像在第一个例子中那样通过写 SummerBlue 来直接访问它)。 我们必须在访问枚举器名称之前写入 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 Colorcolor Blue 分配给变量 c。 所以条件 (c == Color::Blue) 变为真,并且打印出 "Your color is Blue"。在这种情况下,我们比较了 cColor::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

https://www.codesdope.com/cpp-enum-class/

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:00:48  更:2021-08-04 11:01:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 23:22:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码