问题
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