Thread: storing strings by enum/s

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    storing strings by enum/s

    Hello

    What is the best way to store static strings with enums?
    I need to store the error messages in my application with enums:

    Like:
    Code:
    config_error : "could not load config file"
    terminate_error : "could not .. "
    where

    Code:
    enum errors {
    config_error,
    terminate_error,
    ..
    };
    Many thanks in advance!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Typically something like:

    Code:
    char const* 
    text( error code )
    {
          static struct
          {
                error 
                      code;
                char const*
                      text;
          } 
                table[ ] = 
          {
                { config_error, "could not load config file" },
                { terminate_error, "unexpected error - bailing out!" }            
          };
          for( int i = 0, n = sizeof( table ) / sizeof( table[ 0 ] ); i < n; i++ )
                if( code == table[ i ].code )
                      return table[ i ].text;
          return "<undefined error>";       
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    It may also be useful to look up the "typesafe enum pattern"
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values in strings
    By scotty4598 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 10:15 PM
  2. Storing strings in a linked list
    By dws90 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 07:06 PM
  3. Storing long strings?
    By thebudbottle in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 06:10 PM
  4. Storing strings in XML file
    By Nina in forum C++ Programming
    Replies: 0
    Last Post: 03-08-2004, 12:45 PM
  5. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM