mercredi 6 mai 2015

C++ Using an int to set an enum from a list

I am using a header file for character class in a game (doing it as a side project for experience). I have it working but it just feels like I'm doing this the long way. I ask for an 'int' to set the character class then set it based on the enum position using a switch statement. Is there a clearer, shorter way to do this operation? Am I doing anything here that would be considered bad practice?

class Character_Class {
  public:
    enum classofnpc { CLERIC, FIGHTER, ROGUE, WIZARD, BARBARIAN, DRUID, PALADIN, SORCERER, BARD, MONK, RANGER, WARLOCK };

    Character_Class(const int& a, const int& b){
        switch (a) {
            case 0 :
                a_class = CLERIC;
                break;
            case 1 :
                a_class = FIGHTER;
                break;
            case 2 :
                a_class = ROGUE;
                break;
            case 3 :
                a_class = WIZARD;
                break;
            case 4 :
                a_class = BARBARIAN;
                break;
            case 5 :
                a_class = DRUID;
                break;
            case 6 :
                a_class = PALADIN;
                break;
            case 7 :
                a_class = SORCERER;
                break;
            case 8 :
                a_class = BARD;
                break;
            case 9 :
                a_class = MONK;
                break;
            case 10 :
                a_class = RANGER;
                break;
            case 11 :
                a_class = WARLOCK;
                break;
        }
        lvl = b;
    }
  private:
    classofnpc a_class;
    int lvl;
};

Aucun commentaire:

Enregistrer un commentaire