jeudi 4 février 2016

getting c++11 enum class functionnality, with extra static members inside the class

I currently have many classical enums, and static arrays with corresponding strings (for (de)serializing from/to XML for instance)

namespace MyAppCommon
{
    enum Fruit { Banana, Apple, Orange};
    static const char * const fruitString[] = { "banana", "apple", "orange" };
}

In order to avoid increasing name clashes, I would like to use the c++11 enum class which avoids setting a dummy or redundant enum name such as MyAppCommon::Fruit::e or MyAppCommon::Fruit::Fruit

But how could I keep my static array inside the class? Such as:

struct Fruit:
{
    enum e { Banana, Apple, Orange};
    static const char * const string[] = { "banana", "apple", "orange" };
    e value;
}

The problem with this approach, is that I need to reimplement operator=, operator==, etc... to be able to use the enum members items directly, and it won't make the code nicer...

Any idea?

Aucun commentaire:

Enregistrer un commentaire