Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a char to an enum type

I have a piece of code pretty similar to this:

class someclass
{
public:
enum Section{START,MID,END};
vector<Section> Full;
void ex(){
    for(int i=0;i<Full.size();i++)
    {
        switch (Full[i])
        {
        case START :
                  cout<<"S";
                  break;
        case MID :
                  cout<<"M";
                  break;
        case END:
            cout<<"E";
            break;
        }
    }
    }
};

Now imagine I have much more enum types and their names are longer.... well what i get is not a very good looking code and i was wondering if it possible to bind a specific char to an enum type and maybe do something like this:

for(int i=0;i<Full.size();i++)
    {
        cout<(Full[i]).MyChar();
    }

Or any other method that could make this code "prettier". Is this possible?

like image 915
Ravid Goldenberg Avatar asked Jun 13 '13 19:06

Ravid Goldenberg


People also ask

Can an enum be a char?

First of all, YES, we can assign an Enum to something else, a char !

Can you cast a string to an enum?

You can create Enum from String by using Enum. valueOf() method. valueOf() is a static method that is added on every Enum class during compile-time and it's implicitly available to all Enum along with values(), name(), and cardinal() methods.

Can we assign value to enum?

You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

How do I assign an enum to an integer?

To convert an enum to int , we can: Typecast the enum value to int . Use the ToInt32 method of the Convert class.


2 Answers

Unfortunately there is not much you can do to clean this up. If you have access to the C++11 strongly typed enumerator feature, then you could do something like the following:

enum class Section : char {
     START = 'S',
     MID = 'M',
     END = 'E',
};

And then you could do something like:

std::cout << static_cast<char>(Full[i]) << std::endl;

However, if you do not have access to this feature then there's not much you can do, my advice would be to have either a global map std::map<Section, char>, which relates each enum section to a character, or a helper function with the prototype:

inline char SectionToChar( Section section );

Which just implements the switch() statement in a more accessible way, e.g:

inline char SectionToChar( Section section ) {
     switch( section )
     {
     default:
         {
             throw std::invalid_argument( "Invalid Section value" );
             break;
         }
     case START:
         {
             return 'S';
             break;
         }
     case MID:
         {
             return 'M';
             break;
         }
     case END:
         {
             return 'E';
             break;
         }
     }
}
like image 158
Thomas Russell Avatar answered Oct 22 '22 17:10

Thomas Russell


In a situation like this you could be tricky and cast your chars.

enum Section{
    START = (int)'S',
    MID   = (int)'M',
    END   = (int)'E'
};

...

inline char getChar(Section section)
{
    return (char)section;
}
like image 27
Seth Hays Avatar answered Oct 22 '22 15:10

Seth Hays