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

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

问题描述

(问题与我先前的问题相关这里此处此处,然后此处)。

(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



我发现我可以声明一个变量作为枚举类型和设置我的变量等于所定义的枚举值之一。

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 like或C ++ like - 并且最好简单地将变量声明为枚举类型,并摆脱所有的旧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; }

编辑以阻止nitpickers:

Edit to fend off nitpickers:

这只是一个如何实现函数的示例。你不需要使用模板,你可以使用两个模板参数,而不是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; }

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

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