How do I ‘ToString()’ an enum in C++?
In Java and C# I would just call ToString.
enum Colours
{
Red =0,
Green=1,
Blue=2
};
I need to create a string like: "Invalid colour ‘" + colour + "’ selected."
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While this is commonly done through switches, I prefer arrays:
The explicit array size has the benefit of generating a compile time
error should the size of the enum change and you forget to add the
appropriate string.
Alternatively, there is Boost.Enum in the Boost vault. The library
hasn’t been officially released but is quite stable and provides what
you want. I wouldn’t recommend it to a novice though.