Click here to Skip to main content
6,822,613 members and growing! (17,820 online)
Email Password   helpLost your password?
Languages » C / C++ Language » Howto     Intermediate

Converting C++ enums to strings

By Marcos F. Cardoso

How to convert C++ enums to strings.
C, VC7.1, WindowsVS.NET2003, Dev
Posted:27 May 2005
Views:46,282
Bookmarked:21 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 2.86 Rating: 3.00 out of 5
3 votes, 33.3%
1
1 vote, 11.1%
2
2 votes, 22.2%
3
2 votes, 22.2%
4
1 vote, 11.1%
5

Introduction

This code is used to convert a C++ enumeration value to its equivalent string representation. It is useful, for example, to debug your code, or generate trace messages.

Using the code

Create a header file called "EnumToString.h" with the following contents:

// File name: "EnumToString.h"

#undef DECL_ENUM_ELEMENT
#undef BEGIN_ENUM
#undef END_ENUM

#ifndef GENERATE_ENUM_STRINGS
    #define DECL_ENUM_ELEMENT( element ) element
    #define BEGIN_ENUM( ENUM_NAME ) typedef enum tag##ENUM_NAME
    #define END_ENUM( ENUM_NAME ) ENUM_NAME; \
            char* GetString##ENUM_NAME(enum tag##ENUM_NAME index);
#else
    #define DECL_ENUM_ELEMENT( element ) #element
    #define BEGIN_ENUM( ENUM_NAME ) char* gs_##ENUM_NAME [] =
    #define END_ENUM( ENUM_NAME ) ; char* GetString##ENUM_NAME(enum \
            tag##ENUM_NAME index){ return gs_##ENUM_NAME [index]; }
#endif

Put your enum in a specific header file using a special syntax. For example, the enum:

enum Days
{
   sunday,
   monday,
   tuesday,
   wednesday,
   thursday,
   friday,
   saturday
};

will become a header file called "Days.h" with the following contents:

// File name: "Days.h"

#if ( !defined(DAYS_H) || defined(GENERATE_ENUM_STRINGS) )

#if (!defined(GENERATE_ENUM_STRINGS))
    #define DAYS_H
#endif

#include "EnumToString.h"


///////////////////////////////

// The enum declaration

///////////////////////////////

BEGIN_ENUM(Days)
{
    DECL_ENUM_ELEMENT(sunday),
    DECL_ENUM_ELEMENT(monday),
    DECL_ENUM_ELEMENT(tuesday),
    DECL_ENUM_ELEMENT(wednesday),
    DECL_ENUM_ELEMENT(thursday),
    DECL_ENUM_ELEMENT(friday),
    DECL_ENUM_ELEMENT(saturday)
}
END_ENUM(Days)

#endif // (!defined(DAYS_H) || defined(GENERATE_ENUM_STRINGS))

Include the file "Days.h" everywhere you need the enum Days. Use it like a normal enumeration:

#include "Days.h"


void MyFunction( Days day )
{
    CString message;
    switch( day )
    {
        case monday:
        case tuesday:
        case wednesday:
        case thursday:
        case friday:
        {
            message.Format("Today is %s, I have to work!", 
                           GetStringDays(day) );
        }
        break;

        case saturday:
        case sunday:
        {
            message.Format("Today is %s, very nice!!!", 
                           GetStringDays(day) );
        }
        break;
    }
    AfxMessageBox(message);                       
}

Create a CPP module where the strings associated to enums are actually defined:

// File name: "EnumToString.cpp"


/// The strings associated with the enums are gererated here

/////////////////////////////////////////////////////////////////////

#define GENERATE_ENUM_STRINGS  // Start string generation

#include "Days.h"             

#include "OtherEnum.h"

#include "AnotherOne.h"

#undef GENERATE_ENUM_STRINGS   // Stop string generation

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marcos F. Cardoso


Member

Occupation: Web Developer
Location: Brazil Brazil

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralMaking it compile on Linux PinmemberIshmagel22:59 29 Jan '09  
GeneralA different approach PinmemberHugo González Castro18:45 23 Dec '08  
GeneralTry this Pinmembermaddog1012:44 5 Oct '07  
GeneralMaybe you forgotten the most important function! Pinmemberxibeifeijian17:51 25 Mar '07  
GeneralRe: Maybe you forgotten the most important function! PinmemberHugo GC1:29 18 Nov '08  
GeneralWon't work for enums with specified values PinmemberKaushikSridharan10:12 18 Jun '05  
GeneralRe: Won't work for enums with specified values Pinmemberrnadler713014:53 18 Apr '06  
GeneralRe: Won't work for enums with specified values Pinmemberirwin.zuo16:50 30 Oct '06  
GeneralRe: Won't work for enums with specified values PinmemberTroyJ11:10 6 Jul '07  
GeneralRe: Won't work for enums with specified values PinmemberJong Allegraud0:51 13 Aug '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 27 May 2005
Editor: Smitha Vijayan
Copyright 2005 by Marcos F. Cardoso
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project