【问题标题】:declaring the underlying type of enumeration identifier [closed]声明枚举标识符的基础类型
【发布时间】:2012-09-15 10:45:44
【问题描述】:

我已经声明了一个枚举如下:

enum fileType {typeA, typeB};

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

enum fileType : string {typeA, typeB}; 

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

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

【问题讨论】:

  • 在您的链接中This can be any scalar type, such as signed or unsigned versions of int, short, or long. bool or char is also allowed. 您不能使用String 或其他任何内容。

标签: c++ types enums enum-class


【解决方案1】:

您可能只有整数类型作为枚举的基础类型。这意味着有符号和无符号类型,例如 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2012-03-09
    • 2010-10-25
    相关资源
    最近更新 更多