首页 > 编程知识 正文

c语言中枚举类型是什么意思,c语言枚举定义

时间:2023-05-04 08:55:30 阅读:11170 作者:3575

c语言枚举类(enum class )1. enum class//=========================版本:版本1.0.0//版权所有:版权所有(c ) 2020永劫城//描述: hello world in c, ansi-style//============================int main () {Season s=Summer; Color c=Blue; if(s==c ) STD :3360 cout ' equal ' STD :3360 endl; } else { STD :3360 cout ' not equal ' STD 33603360 endl; }返回0; (} Here,wewanttocheckwhethersummerisequaltobluewhichisobviouslynottruesinceoneofthesseasonandtheotheracolor.intheaboveveve wegottheoutputtruebecausesummerandbluegotconvertedintointegersandthenthoseintegersgotcompared。

在此检查Summer是否等于Blue。 这显然不是真的。 因为一个是Season,另一个是Color。 在上面的示例中,Summer和Blue转换为整数,并比较这些整数,从而获得输出true。

Equal请按任意键继续。 thevaluesofbothsummerandblueis0andthusthevaluesofsandcbecame0. so,thecondition(s==c ) )。 becametrueandequalgotprintedonthescreen.thisisnotthedesiredoutputbecausewecannotcompareaseasonandacolor.sinceseasonanandcolor ifferent enumerations,thereforetheseshouldnotbecompared。

因为Summer和Blue的值为0,所以s和c的值为0。 因此,条件(s==c )为true,印刷在Equal画面上。 这不是所需的输出,因为无法比较Season and Color。 请勿比较Season and Color,因为它是不同的枚举。

herecomesinenumclasswhichlimitsthescopeoftheenumeratorswithintheirenums.so, nowanyenumeratorwillbeknownbyitsenumthuslimitingitsscopewitheenumtowhichitbelongs.thisisthereasonenumclasssisalsocalllledsccccon

这里有enum class,它限制了枚举中枚举数据的范围。 因此,当前任何枚举数据都是在其枚举中已知的,所以其范围限制在所属的枚举内。 这就是枚举类也称为作用域枚举的原因。

To make an enum class,wesimplyhavetoaddthekeywordclassafterthekeywordenum。

要创建枚举类,请在关键字枚举之后添加关键字类。

enum class Season{Summer,Spring,Winter,Autumn}; wemadeourenumseasonanenumclassbyaddingthekeywordclassafterthekeywordenum.now let’sseehowtoaccessanyelement (

用关键字enu

m 之后添加关键字 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.cpp1>d:visual_studio_workspacehash_tablehash_tableyongqiang.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.
在这个例子中,我们不能直接访问枚举数据 (即我们不能像在第一个例子中那样通过写 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

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。