PDA

View Full Version : C++ print enumeration constant name?



errcricket
December 26th, 2008, 02:21 PM
I am having some problems with enumerations. Lets say for example I have defined a new data type:


enum
Vehicle {SMART = 0, MINI = 1, MAZDA = 2, ACCORD = 3} ; I know that I can assign each constant a value, and that if none is supplied, the compiler will assign the first constant a value of zero with each constant increasing by a value of 1. …what if I want to print the actual constant name? instead of 2, I want MAZDA instead? Is there a way to do this without strings?

Krilnon
December 26th, 2008, 04:55 PM
No, because the constant name is a string and you can't print a string without a string to print.

You could create an array that was the reverse of that enum, kind of, by making the zeroth index "SMART, and etc., but that would use strings, of course. I'm guessing that the actual constant names don't survive the compilation process, so you wouldn't have access to them unless you hard coded them in as strings yourself.