声明枚举标识符的基础类型

declaring the underlying type of enumeration identifier

本文关键字:类型 枚举 标识符 声明      更新时间:2023-10-16

我声明了一个枚举,如下所示:

enum fileType {typeA, typeB};

当我尝试将目录类型附加到字符串时,这会导致错误。我认为我需要在枚举声明中包含枚举标识符的基础类型。或类似的东西

enum fileType : string {typeA, typeB}; 

如 http://msdn.microsoft.com/en-US/library/2dzy4k6e(v=vs.80) 中所述.aspx

但是这不是为我编译的。声明枚举标识符的基础类型的正确语法是什么?

您可能只有整型类型作为枚举的基础类型。这意味着有符号和无符号的类型,如 char short intlong .

枚举的名称在运行时中不可用。如果要显示它们(或附加到字符串),则必须编写特殊代码。

 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];