declaring the underlying type of enumeration identifier [closed]

ⅰ亾dé卋堺 提交于 2019-12-12 07:00:42

问题


I have declared an enum as follows:

enum fileType {typeA, typeB};

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

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?


回答1:


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


来源:https://stackoverflow.com/questions/12557164/declaring-the-underlying-type-of-enumeration-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!