声明枚举标识符的基础类型
declaring the underlying type of enumeration identifier
我声明了一个枚举,如下所示:
enum fileType {typeA, typeB};
当我尝试将目录类型附加到字符串时,这会导致错误。我认为我需要在枚举声明中包含枚举标识符的基础类型。或类似的东西
enum fileType : string {typeA, typeB};
如 http://msdn.microsoft.com/en-US/library/2dzy4k6e(v=vs.80) 中所述.aspx
但是这不是为我编译的。声明枚举标识符的基础类型的正确语法是什么?
您可能只有整型类型作为枚举的基础类型。这意味着有符号和无符号的类型,如 char
short
int
和 long
.
枚举的名称在运行时中不可用。如果要显示它们(或附加到字符串),则必须编写特殊代码。
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];
相关文章:
- 在有符号基础类型枚举的位域上溢出
- 在编译时将强类型枚举器转换为其基础类型?
- 如果 int 是"not within the enums range",为什么将 int 转换为强类型枚举会编译?
- 类型枚举的变量不是类型名称
- 强类型枚举的语法实现错误
- 枚举与强类型枚举
- 错误:T没有命名类型-用于使用强类型枚举的专门化
- 如何使用强类型枚举
- QT:将强类型枚举参数传递到插槽
- 在类定义中声明类型(枚举、结构等)会增加代码大小
- Visual Studio 11 (beta) 中的强类型枚举类
- 使用强类型枚举对类型和子类型进行建模
- 不允许将强类型枚举用作同一基础类型的参数
- 如何正确使用C++强类型枚举
- 带有强类型枚举的模板参数推导
- 是否可以在googleprotobuf中为类型(枚举或消息)定义一个别名
- C++中默认初始化的全局强类型枚举是什么
- std::is_signed不适用于强类型枚举:int
- 如何定义自定义值的类型?(枚举类型定义)
- 将强类型枚举传递给函数