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

标签 c++ arrays enums static-methods code-organization

我通过将他的工作设置为一个数字来跟踪玩家的“工作”,如果他换工作则将其递增 1,并根据数字是偶数还是奇数来确定他当前的工作。 (现在只有两份工作)。但是,我知道有更好的方法来执行此操作,很快我将需要实现第三个和第四个作业,因此我不能继续使用偶数/奇数检查。

这是我的引用代码:(请注意,我只包含相关代码)

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]);

关于c++ - 有什么比使用静态整数更好的存储信息的方法? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5664199/

相关文章:

c++ - 显式转换运算符 bool

c++ - 如何获取当前函数的标识符?

php - Mysql 结果作为数组

c# - 所有枚举项到字符串 (C#)

java - java枚举器中的私有(private)构造函数

c++ - 函数模板特化和 Abrahams/Dimov 示例

c++ - 动态规划 - 事件选择

javascript - 过滤包含数组的对象数组

c - 将数组传递给函数

c# - 任何人都知道缺少枚举通用约束的良好解决方法?