Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7407775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:47:03+00:00 2026-05-29T05:47:03+00:00

My current program uses 3 different enums: enum ThingEnum{ Thing1 = 0, Thing2 =

  • 0

My current program uses 3 different enums:

enum ThingEnum{
    Thing1 = 0,
    Thing2 = 1,
    Thing3 = 2,
    OtherThing = 3
};
enum ReturnEnum {
    Success = 4,// the method did what it is supposed to
    Error1 = 5,// an error occured
    Error2 = 6,// an error occured
    Error3 = 7,// an error occured
    Error4 = 8,// a fatal error occured program must terminate
    Pointer = 9// the method may need to be called again
};
enum TypeEnum {
    Type1 = 10,
    Type2 = 11,
    Type3 = 12,
    Type4 = 13,
    OtherType = 14
};  

What I want to do is create a global function that takes an enum and returns a string (as the value of an enum is really just a specialized variable name that always has a value). Is it possible to create a function that takes a generic enum? e.g.

string enumToString (enum _enum){}

or would I have to make a function for each different enum? Just a possible thought I did some reading, and some compilers allow for a Enum to resolve to an int so could I pass the enum as an int and then work with it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:47:04+00:00Added an answer on May 29, 2026 at 5:47 am

    There are two options of “ToString-like” function implementation:

    1. Implement a simple static switch-case function.

    Code:

    std::string ThingEnumToString(ThingEnum thing)
    {
        switch (thing) {
        case Thing1:
            return std::string("Thing1");
        case Thing2:    
            return std::string("Thing2");
        case Thing3:
            return std::string("Thing3");        
        case OtherThing:
            return std::string("OtherThing");
        default:
            throw std::invalid_argument("thing");
            break;
        }
    }
    
    1. Implement a static function with a dictionary lookup.

    Code:

    typedef std::map<ThingEnum, std::string> ThingsMap;
    
    static ThingsMap GetThingsMap()
    {
        ThingsMap things_map;
        things_map.insert(ThingsMap::value_type(Thing1, std::string("Thing1")));
        things_map.insert(ThingsMap::value_type(Thing2, std::string("Thing2")));
        things_map.insert(ThingsMap::value_type(Thing3, std::string("Thing3")));
        things_map.insert(ThingsMap::value_type(OtherThing, std::string("OtherThing")));
        return things_map;
    }
    
    static std::string ThingEnumToString(ThingEnum thing)
    {
        static const ThingsMap things(GetThingsMap());
        ThingsMap::const_iterator it = things.find(thing);
        if (it != things.end()) {
            return it->second;
        } else {
            throw std::invalid_argument("thing");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My current program need to use programatically create a XPathExpression instance to apply to
http://img136.imageshack.us/img136/3508/texturefailz.png This is my current program. I know it's terribly ugly, I found two
I need to use the current library name explicitly in a CL program. CHGVAR
How can I change the current working directory from within a Java program? Everything
I am trying to get the program to call up the current date, add
Using my c++ program how can I find out what group the current user
How can I avoid that a user starts the same program twice? The current
I have a program that edits images in different ways... i have a Paint
I am trying to write a program that uses a Berkeley Database and i
I'm having a hard time describing a Flash program that uses a sort of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.