CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Enum <-> String

Hybrid View

  1. #1
    Join Date
    May 2002
    Posts
    31

    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?

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  3. #3
    Join Date
    May 2002
    Posts
    31

    Re: Enum <-> String

    Any better approaches...?

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Enum <-> String

    Quote 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

  6. #6
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    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.

  7. #7
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378

    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
  •  





Click Here to Expand Forum to Full Width

Featured