(问题与我之前的问题相关here https://stackoverflow.com/questions/3238718/difference-between-const-variable-and-const-type-variable, here https://stackoverflow.com/questions/3231481/enum-element-limit, here https://stackoverflow.com/questions/3199761/define-bit0-bit1-bit2-etc-without-define, and here https://stackoverflow.com/questions/3197913/size-of-define-values).
我正在维护一个非常古老的应用程序,该应用程序是多年前从 DOS 移植到 Windows 的,但许多旧的 C 约定仍然保留了下来。
一个特定的约定是 setBit 和 clrBit 宏:
#ifndef setBit
#define setBit(word, mask) word |= mask
#endif
#ifndef clrBit
#define clrBit(word, mask) word &= ~mask
#endif
我发现我可以将变量声明为枚举类型,并将变量设置为等于定义的枚举值之一。
enum SystemStatus
{
SYSTEM_ONLINE = BIT0,
SYSTEM_STATUS2 = BIT1,
SYSTEM_STATUS3 = BIT2,
SYSTEM_STATUS4 = BIT3
};
With BIT0 = 0x00000001
, BIT1 = 0x00000002
, etc.
SystemStatus systemStatus;
systemStatus = SYSTEM_ONLINE
在您看来,使用 setBit 和 clrBit 宏更像是 C 还是 C++?简单地将变量声明为枚举类型并摆脱所有旧的 setBit/clrBit 内容会更好吗?