有什么比使用静态整数更好的信息存储方式? C ++

| 我通过将玩家的工作设置为数字来跟踪他的“工作”,如果改变工作将其递增一个,并通过数字是偶数还是奇数来确定他当前所从事的工作。 (现在只有两项工作)。但是,我知道这样做的更好方法,很快我将需要执行第三和第四项工作,因此我无法继续使用偶/奇检查。 这是我的代码供参考:(请注意,我只包含相关代码) GameModeState.cpp
// If changeJob\'s parameter number is 1, it increments the job. If number is 2, it only  returns the current job
int GameModeState::changeJob(int number)
{
   // Default job is even (landman)
   static int job = 1;
   if (number == 1)
   {
    job = (job+1);
    return job;
   } 
   else 
   {
    return job;
   }
}

int GameModeState::getJob()
{
    int currentJob = (changeJob(2));
    return currentJob;
}

// If the player opens the \"stat sheet\", it changes their job
void GameModeState::_statSheet(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
    changeJob(1);
}
GameModeState.h
class GameModeState : public GameState::State
{
public:

    /// Changes the player\'s job if number is 1, or returns current job if number is 2
    static int changeJob(int number);

    /// Returns the current job number by calling changeJob appropriately
    static int getJob();

private:

    // Opening the player sheet will change the player\'s job
    void _statSheet(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output);
};
ZoneMovementState.cpp(这是我检查当前作业的位置)
#include \"GameModeState.h\"
#include <EnergyGraphics/ZoneParser.h>

    void ZoneMovementState::_changeZone(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
    {
        // If the number from getJob is even, the player is currently a geologist
        if (GameModeState::getJob()%2 == 0)
        {
            ZoneParser::getSingleton().load(\"../media/zones/geology_zone.xml\", false);
        } 
        else //otherwise they are a landman
        {
            ZoneParser::getSingleton().load(\"../media/zones/landman_zone.xml\", false);
        }
        transitionHandler->go();
    }
我认为作业的数组或枚举将是处理此问题的更好方法,但是我不确定如何将其实现到我的代码中。如果您知道更好的方法,请提供示例或至少指向正确方向的一点。我将不胜感激!     
已邀请:
不要使用静态变量将此类内容保存在类中。请改用成员变量。 IMO做这样的事情并使它可扩展的最简单方法是使用枚举:
enum PlayerJob
    JOB_NONE = 0,
    JOB_GEOLOGIST,
    JOB_LANDMAN,
    ...
    NUM_JOBS // this element is optional but can be useful for range checking.
};

...

PlayerJob job = JOB_NONE;

...

switch(job)
{
    case JOB_NONE:
        break;
    case JOB_GEOLOGIST:
        ...
        break;
    ...
    default:
        error(\"Unhandled palyer job: %d\", job);
        break;
}
另外,我会考虑以某种方式将此类“与工作相关”的东西组织到某种数组或列表中,或将其更容易地称为“与工作相关的”东西:
std::map<PlayerJob,std::string> jobzones;
jobzones.push_back(JOB_GEOLOGIST, \"geozone.xml\");

...

transitToZone(jobzones[job]);
    
枚举很好,您也可以考虑对GameState使用ѭ5或类似的符号,以便可以进行下推/弹出操作。     
您可能想看看状态模式。     

要回复问题请先登录注册