1

If I have an enumeration like

enum values {


   var1 = 12,

   var2 = 23,

   var3 = 34
};

is it possible to get the value from

  string str = "var2";

  // Get values::str;

I have tried creating an enum variable and assigning the variable str to it, but this gives a type mismatch error.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • https://stackoverflow.com/questions/207976/how-to-easily-map-c-enums-to-strings – NathanOliver Jul 13 '22 at 02:39
  • The names only exist at compile-time. You would have to add code to keep a runtime map of names to values if that's what you wanted . – M.M Jul 13 '22 at 02:41
  • No, not in C++. Some languages like Java and Typescript provide reflection capabilities to get the names and underlying data for enum constants, but in C++ they truly are integers with extra steps. None of that information is available at runtime. – Silvio Mayolo Jul 13 '22 at 02:51

0 Answers0