Glen Morse
Glen Morse

Reputation: 2603

Making Enum work

In player.h I have enum race {Living, Dead, Nature,None};

In player.cpp I have

race myRace;
    void Player::addPlayer(int x)
 {
    cout<<"added player"<<x<<endl;
    this->karma=0;
    this->myTurn=false;
    myRace=race(4);

 }

So this should , when a player is created make myRace = None.

Now in Main.cpp I want to check a players race, how would i do this? I tried if (player[x].myRace=None) but of course that wont work..

Upvotes: 0

Views: 176

Answers (4)

Petr Skocik
Petr Skocik

Reputation: 60163

The whole point of enums is to do away with magical numbers, so although enums are convertible to integers, getting enums from integers is counter productive. You should write myRace=None, rather than myRace=4.

Secondly unless, you explicitly specify otherwise in your declaration of your enum type, enums starts at 0, so None corresponds to 3 rather than 4.

Thirdly, in C++ you don't have to write this->member_name to access a member variable. If you want to differentiate members from nonmembers, you can save many keystrokes by adopting a much shorter naming convention. (Such as appending _ to member variable names).

Finaly, = in C++ means assignment, NOT equality comparison. if (player[x].myRace=None) effectively means player[x].myRace=None; if (player[x].myRace), i.e. the condition is always true, since player[x].myRace==None==3. Most of the time, the assignment operator inside if conditions is an error and your compiler might warn you about this.

Also, it's kind of weird for a member variable to access your myRace global (and globals are usually a bad idea). If myRace is not a global, then a function can access it only if a pointer or a reference is passed to it as an argument or if myRace is made as a member variable and your function is a method of the same class.

Upvotes: 2

kfsone
kfsone

Reputation: 24269

Enums should work the way you describe. Unfortunately you only provided modified, unrunnable code and no compiler errors or other output. However, the following code should show you how to use enums: (Live demo)

#include <iostream>

enum Race { None, Living, Dead, Nature}; // always put invalid as the 0 entry.

class Player
{
      uint32_t m_karma;
      bool     m_myTurn;
      Race     m_myRace;

public:
    void addPlayer(int x_)
    {
        std::cout << "added player " << x_ << std::endl;
        m_karma = 0;
        m_myTurn = false;
        m_myRace = None;
    }
};

int main(int argc, const char** argv)
{
    Player fred;
    fred.addPlayer(1);

    return 0;
}

If you have a C++11 capable compiler you can be more specific and use "enum Class" which will force you to qualify enumerations (i.e. Race::None). (Live demo)

#include <iostream>

enum class Race { None, Living, Dead, Nature}; // always but invalid as the 0 entry.

class Player
{
      uint32_t m_karma;
      bool     m_myTurn;
      Race     m_myRace;

public:
    void addPlayer(int x_)
    {
        std::cout << "added player " << x_ << std::endl;
        m_karma = 0;
        m_myTurn = false;
        m_myRace = Race::None;
    }
};

int main(int argc, const char** argv)
{
    Player fred;
    fred.addPlayer(1);

    return 0;
}

"player[x].race = None" won't work because that is assignment, not a test for equality. You don't say why it didn't work, I'm going to assume - if it wasn't just the wrong variable name - because it was private or inaccessible. In that case, just add a member function to return the race:

class Player {
...
public:
    Race getRace() const { return m_myRace; }
    ...
};

Upvotes: 2

Kek
Kek

Reputation: 3195

Be careful here ! myRace is not part of Player class. So you will have one instance of myRace, whatever the number of players. You should make myRace part of your class. If you don't, everytime you create a new player, the race changes !

If this is what you want, it should be a static member of your player class, so you could add a static getter tro retrieve it

class Player{

    static int myRace;

public:
    static int GetMyRace(){
        return myRace;
    }
    ...
}

and then, access it like this :

Player::GetMyRace();

Upvotes: 1

Zlatomir
Zlatomir

Reputation: 7044

This should work: myRace = None; and it's the recommended way to use an enum to avoid another mistake that you made, 4 is not a valid integer option for your enum, because Living is 0 and None will be 3.

Upvotes: 3

Related Questions