Tonto: Yes, I know that enum values are integers.
Daved: I want instances of the State class to be able to return enum values instead of the integers it stores.
This is what I want to do (but in C++). And I know it uses reflection and an unchecked cast, but all I want to know is if this is possible in C++ at all.
Code:
import java.util.Map;
import java.util.HashMap;
public class State {
public Map<String, Integer> values = new HashMap<String, Integer>();
public enum MyEnum {MY_CONSTANT};
public <E extends Enum<E>> E getEnumValue(String key, Class<E> enumType) {
try {
E[] enumValues = (E[])enumType.getMethod("values").invoke(null);
return enumValues[values.get(key)];
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
State s = new State();
s.values.put("!", State.MyEnum.MY_CONSTANT.ordinal());
System.out.println(s.getEnumValue("!", State.MyEnum.class));
System.out.println(s.getEnumValue("!", State.MyEnum.class).getClass().getName());
}
}