C ++ #defining枚举状态

时间:2018-02-10 00:10:14

标签: c++ enums refactoring

我想知道当我将我的枚举的所有状态定义为较短的同行时我是否做出了一个好的决定:只是为了整理代码。
代码:
枚举:

enum class ESelectedCharacterState : uint8
{
    SS_WantsWalk,
    SS_WantsJog,
    SS_WantsCrouch,
    SS_WantsProne,
    SS_WantsJump
};

定义:

#define WantsWalk ESelectedCharacterState::SS_WantsWalk
#define WantsJog ESelectedCharacterState::SS_WantsJog
#define WantsCrouch ESelectedCharacterState::SS_WantsCrouch
#define WantsProne ESelectedCharacterState::SS_WantsProne
#define WantsJump ESelectedCharacterState::SS_WantsJump

没有#defined枚举状态的情况:

switch (StateSelected)
    {
    case ESelectedCharacterState::SS_WantsWalk:
        break;
    case ESelectedCharacterState::SS_WantsJog:
        break;
    case ESelectedCharacterState::SS_WantsCrouch:
        break;
    case ESelectedCharacterState::SS_WantsProne:
        break;
    case ESelectedCharacterState::SS_WantsJump:
        break;
    default:
        break;
    }

#defined枚举的情况:

switch (StateSelected)
    {
    case WantsWalk:
        break;
    case WantsJog:
        break;
    case WantsCrouch:
        break;
    case WantsProne:
        break;
    case WantsJump:
        break;
    default:
        break;
    }

这实际上只是一小段代码,但我在项目中经常使用这个枚举。

1 个答案:

答案 0 :(得分:0)

通过缩短typedef的类型,使用enum可以更清晰地实现此目的。

typedef ESelectedCharacterState ESCS;