-
August 31st, 2005, 02:08 AM
#1
Enum <-> String
Do we have any general purpose C++ class to convert a enumeration value to corresponding string?
Not by mapping to string tables or maps... If we have 100's of enumeration it is just impossible to define corresponding strings to enums....
I have seen some implementation in .NET
---- Enum::GetName
enum Styles
{
one=1,
two,
three,
four
};
Enum::GetName(__typeof(Styles), __box(3)));
This returns the corresponding name from the enum itself 'three'....
Do we have any similar class for using in normal C++ application not depending on any MS specific libraries?
-
August 31st, 2005, 02:16 AM
#2
Re: Enum <-> String
I will go out on a limb here and say:
No
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
August 31st, 2005, 07:00 AM
#3
Re: Enum <-> String
Any better approaches...?
-
August 31st, 2005, 07:43 AM
#4
Re: Enum <-> String
If you are not compiling your program with debug information, the running binary is unlikely to know what name you used in the source code for an enum value.
If you need the behavior, I would recommend writing yourself an Enum class that implements that behavior.
-
August 31st, 2005, 09:47 AM
#5
Re: Enum <-> String
Originally Posted by chanduonline
Do we have any general purpose C++ class to convert a enumeration value to corresponding string?
Yes, it's called a map.
Not by mapping to string tables or maps
So you are not looking for a class. You're looking for whether this is built into the language itself. The answer is no, C++ has no "reflection" capabilities similar to Java or ways of turning a variable into its string equivalent.
Regards,
Paul McKenzie
-
August 31st, 2005, 10:17 AM
#6
Re: Enum <-> String
Well, as everyone else has already stated the answer is NO. But since the enum you posted composed of string representation of numbers. You can do something like
Code:
enum some_enum
{
zero,
one,
two,
three
};
int main(void)
{
std::stringstream SStr;
enum some_enum se = one;
SStr << se; // Implicitly converted to int here
std::cout << SStr.str() << std::endl;
}
It's not of much use, when you have members of your enum named different like car, cycle etc etc.
HTH,
Regards,
Usman.
-
August 31st, 2005, 12:52 PM
#7
Re: Enum <-> String
I know this might be inefficient but I used CStringArray for the purpose.
þ|êâšë rä†è rëþ|ïëš †hª† hë|þëd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|