枚举或定义,我应该使用哪一个?

时间:2021-07-06 20:06:38

enum and #define appears to be able to do the same thing, for the example below defining a style. Understand that #define is macro substitution for compiler preprocessor. Is there any circumstances one is preferred over the other?

对于定义样式的示例,枚举和#define似乎能够做同样的事情。理解#define是编译器预处理器的宏替换。有什么情况比另一个更受欢迎吗?

typedef enum {
    SelectionStyleNone,
    SelectionStyleBlue,
    SelectionStyleRed
} SelectionStyle;

vs

#define SELECTION_STYLE_NONE 0
#define SELECTION_STYLE_BLUE 1
#define SELECTION_STYLE_RED  2

6 个解决方案

#1


11  

Don't ever use defines unless you MUST have functionality of the preprocessor. For something as simple as an integral enumeration, use an enum.

除非必须具有预处理器的功能,否则不要使用定义。对于像整数枚举这样简单的东西,请使用枚举。

#2


3  

An enum is the best if you want type safety. They are also exported as symbols, so some debuggers can display them inline while they can't for defines.

如果你想要类型安全,enum是最好的。它们也作为符号导出,因此一些调试器可以内联显示它们,而不能用于定义。

The main problem with enums of course is that they can only contain integers. For strings, floats etc... you might be better of with a const or a define.

枚举的主要问题当然是它们只能包含整数。对于字符串,浮点数等...使用const或define可能会更好。

#3


2  

The short answer is that it probably doesn't matter a lot. This article provides a pretty good long answer:

简短的回答是它可能并不重要。这篇文章提供了一个非常好的长答案:

http://www.embedded.com/columns/programmingpointers/9900402?_requestid=345959

#4


1  

When there's a built-in language feature supporting what you want to do (in this case, enumerating items), you should probably use that feature.

如果有一个内置的语言功能支持你想要做的事情(在这种情况下,枚举项目),你应该使用该功能。

#5


1  

Defines are probably slightly faster (at runtime) than enums, but the benefit is probably only a handful of cycles, so it's negligible unless you're doing something that really requires that. I'd go with enum, since using the preprocessor is harder to debug.

定义可能比枚举更快(在运行时),但好处可能只是少数几个周期,所以除非你做的事情真的需要,否则它可以忽略不计。我会使用枚举,因为使用预处理器更难调试。

#6


0  

#define DEFINED_VALUE 1
#define DEFINED_VALUE 2 // Warning
enum { ENUM_VALUE = 1 };
enum { ENUM_VALUE = 2 }; // Error

With #define, there is a higher probability of introducing subtle bugs.

使用#define,引入细微错误的可能性更高。

#1


11  

Don't ever use defines unless you MUST have functionality of the preprocessor. For something as simple as an integral enumeration, use an enum.

除非必须具有预处理器的功能,否则不要使用定义。对于像整数枚举这样简单的东西,请使用枚举。

#2


3  

An enum is the best if you want type safety. They are also exported as symbols, so some debuggers can display them inline while they can't for defines.

如果你想要类型安全,enum是最好的。它们也作为符号导出,因此一些调试器可以内联显示它们,而不能用于定义。

The main problem with enums of course is that they can only contain integers. For strings, floats etc... you might be better of with a const or a define.

枚举的主要问题当然是它们只能包含整数。对于字符串,浮点数等...使用const或define可能会更好。

#3


2  

The short answer is that it probably doesn't matter a lot. This article provides a pretty good long answer:

简短的回答是它可能并不重要。这篇文章提供了一个非常好的长答案:

http://www.embedded.com/columns/programmingpointers/9900402?_requestid=345959

#4


1  

When there's a built-in language feature supporting what you want to do (in this case, enumerating items), you should probably use that feature.

如果有一个内置的语言功能支持你想要做的事情(在这种情况下,枚举项目),你应该使用该功能。

#5


1  

Defines are probably slightly faster (at runtime) than enums, but the benefit is probably only a handful of cycles, so it's negligible unless you're doing something that really requires that. I'd go with enum, since using the preprocessor is harder to debug.

定义可能比枚举更快(在运行时),但好处可能只是少数几个周期,所以除非你做的事情真的需要,否则它可以忽略不计。我会使用枚举,因为使用预处理器更难调试。

#6


0  

#define DEFINED_VALUE 1
#define DEFINED_VALUE 2 // Warning
enum { ENUM_VALUE = 1 };
enum { ENUM_VALUE = 2 }; // Error

With #define, there is a higher probability of introducing subtle bugs.

使用#define,引入细微错误的可能性更高。