stdx::Enum Class Reference
Standard C++ enums have some problems including the type-safety issues, lack of a containing namespace, the inability to easily print enums, and no support for extending an enum. The following simple class solves these problems with relatively little burden on the programmer. An example is below:
\\ In the header file struct Fruit : public stdx::Enum { static const Fruit apple; static const Fruit orange; static const Fruit pear; protected: Fruit( const char* name ) : stdx::Enum( name ) { } }; \\ In the source file STDX_INIT_ENUM( Fruit, apple ); STDX_INIT_ENUM( Fruit, orange ); STDX_INIT_ENUM( Fruit, pear );
An enumeration should inherit from stdx::Enum and provide a a single constructor which takes a character string as input. The enumeration should contain static const members for each possible enum value. The helper macros in the source file initialize the static members and also makes sure the members get passed strings reflecting their value. This makes it easy to print out the value of an enum.
Inheritance diagram for stdx::Enum:

Protected Methods
- Enum (const char *name)
Friends
- operator<< (std::ostream &, const Enum &)
- operator== (const Enum &e1, const Enum &e2)
- operator!= (const Enum &e1, const Enum &e2)
Related Functions
(Note that these are not member functions.)- STDX_INIT_ENUM(type_, name_)