枚举与宏状态C ++ [英] Enum vs Macro States C++

查看:176
本文介绍了枚举与宏状态C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(问题与我之前的问题有关 here 此处 here 这里)。

(Question is related to my previous questions here, here, here, and here).

我正在维护几年前从DOS移植到Windows的旧应用程序,但是很多旧的C约定仍然继续。

I am maintaining a very old application that was ported years ago from DOS to Windows, but a lot of the old C conventions still carry forward.

一个特定的约定是一个setBit和clrBit宏:

The one particular convention is a setBit and clrBit macro:

#ifndef setBit
#define setBit(word, mask) word |= mask
#endif

#ifndef clrBit
#define clrBit(word, mask) word &= ~mask
#endif

我发现我可以将一个变量声明为枚举类型并设置我的变量等于o

I found that I could declare a variable as an enum type and set my variable equal to the one of the enumerated values that are defined.

enum SystemStatus
{
    SYSTEM_ONLINE                = BIT0,
    SYSTEM_STATUS2               = BIT1,
    SYSTEM_STATUS3               = BIT2,
    SYSTEM_STATUS4               = BIT3
};

使用 BIT0 = 0x00000001 BIT1 = 0x00000002 等。

SystemStatus systemStatus;

systemStatus = SYSTEM_ONLINE

在您看来,正在使用setBit和clrBit宏更像C或C ++,而最好简单地将变量声明为枚举类型,并摆脱所有旧的setBit / clrBit的东西?

In your opinion, is using the setBit and clrBit macros more C like or C++ like - and would it be better to simply declare variables as an enumerated type and get rid of all the old setBit/clrBit stuff?

推荐答案

我觉得你的目的令人困惑。枚举是关于设置要用作标志的值。 setBit和clrBit是关于数据操作的。这个数据可能会是一个标志,但这真的是两个想法之间唯一的关系。

I think you're confusing the purposes. The enum is about setting up values to use as flags. setBit and clrBit are about operating on data. That data might happen to be a flag, but that's really the only relationship between the two ideas.

就是说,宏当然不是C ++的做事方式。您将使用内联函数。例如:

That being said, macros are certainly NOT the C++ way of doing things. You would use inline functions instead. For example:

template<typename T>
inline T& setBit(T& word, T mask) { return word |= mask; }

template<typename T>
inline T& clrBit(T& word, T mask) { return word &= ~mask; }

编辑以防止硝化剂:

这只是一个如何实现这些功能的例子。您不需要使用模板,您可以使用两个模板参数而不是1,如果需要,您可以使用一个void函数或值而不是引用(尽管这会丢失一些原始语义)。主要的一点是获得类型安全的好处,你不会在宏中找到(在宏的许多其他缺点之中)。 http://www.parashift.com/c++- faq-lite / inline-functions.html#faq-9.5

This is just an example of how you could implement the functions. You don't need to use templates, you can use two template parameters instead of 1, you can use a void function or values instead of references if you want, (though then it would lose some of the original semantics). The main point is to get the benefits of type safety which you won't find in macros (among many other downsides of macros). http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5

编辑:这是一个无效的非模板版本进行比较

Here's a void, non-template version for comparison

inline void setBit(unsigned int word, unsigned int mask) { word |= mask; }

inline void clrBit(unsigned int word, unsigned int mask) { word &= ~mask; }

这篇关于枚举与宏状态C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆