storing strings by enum/s

This is a discussion on storing strings by enum/s within the C++ Programming forums, part of the General Programming Boards category; Hello What is the best way to store static strings with enums? I need to store the error messages in ...

  1. #1
    l2u
    l2u is offline
    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,655
    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 <ip.hpp>

  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, 06:06 PM
  3. Storing long strings?
    By thebudbottle in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 05:10 PM
  4. Storing strings in XML file
    By Nina in forum C++ Programming
    Replies: 0
    Last Post: 03-08-2004, 11:45 AM
  5. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21