简体   繁体   English

在“枚举”和“枚举类”中将枚举值转换为 integer

[英]Convert Enum value to integer in “Enum” and “Enum Class”

What is the difference between the Enum and Enum Class and how to converting Enum value to the integer in "Enum" and "Enum Class"? Enum 和 Enum Class 有什么区别,以及如何在“Enum”和“Enum Class”中将 Enum 值转换为 integer?

C++ has two kinds of enum: C++ 有两种枚举:

enum classes枚举类
Plain enums普通枚举
Here are a couple of examples on how to declare them:以下是一些关于如何声明它们的示例:

 enum class Color { red, green, blue }; // enum class
 enum Animal { dog, cat, bird, human }; // plain enum 

What is the difference between the two?两者有什么区别?

enum classes - enumerator names are local to the enum and their values do not implicitly convert to other types (like another enum or int)枚举类 - 枚举器名称是枚举的本地名称,并且它们的值不会隐式转换为其他类型(如另一个枚举或 int)

Plain enums - where enumerator names are in the same scope as the enum and their values implicitly convert to integers and other types普通枚举 - 其中枚举器名称与枚举在同一 scope 中,并且它们的值隐式转换为整数和其他类型

in the Enum:在枚举中:

enum type{x=10,y,z=50,j};

int value = x;

in the Enum Class:在枚举 Class 中:

enum class type{x=10,y,z=50,j};

int value= static_cast<int>(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM