I want my program read the file and when encounter any enum type, instead of treating it as a string, I want the program store it in memory as a enum value.
How can I do that? Thanks!
1 2 3
string s;
ifs>>s;
s // how to convert it to enum???
For a straight-up enum, just read the string and compare it to an array of strings. The index of the match is the enum value, and a simple cast will get the proper value.
For an enum with arbitrary values, you also need to provide a lookup table to convert between the match index and the enum value.
For typed enums, you can do it directly in an overloaded operator >>, but for the old enums I would wrap it in an input manipulator.