声明枚举标识符的底层类型 [英] declaring the underlying type of enumeration identifier
问题描述
我已经声明了一个枚举如下:
I have declared an enum as follows:
enum fileType {typeA, typeB};
当我尝试将directoryType类型附加到字符串时,会导致错误。
我相信我需要在枚举声明中包含枚举标识符的基本类型。或类似
This is causing an error when I try to append directoryType type to a string. I believe I need to include the underlying type of the enumeration identifiers in the enum declaration. Or something like
enum fileType : string {typeA, typeB};
/library/2dzy4k6e(v=vs.80).aspxrel =nofollow> http://msdn.microsoft.com/en-us/library/2dzy4k6e(v = vs.80).aspx
as described in http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=vs.80).aspx
但是这不是为我编译。
however this is not compiling for me. What is the proper syntax for declaring the underlying type of enum identifiers?
推荐答案
您可能只有整数类型作为枚举的底层类型。这意味着签名和未签名的类型,如 char
short
int
long
。
You may have only integral types as underlying type for enum. That means signed and unsigned types like char
short
int
and long
.
枚举名称无法在运行时使用。
The names of enumerations are nowhere available runtime. If you want to display them (or append to string) then you have to write special code.
enum fileType {typeA, typeB};
const char *fileType_str[]={ "typeA","typeB"};
fileType x = typeA;
// display x
std::cout << "x is " << fileType_str[x] << std::endl;
// append x to string
std::string y = "directoryType type to a ";
y += fileType_str[x];
这篇关于声明枚举标识符的底层类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!