Difference between Enum and Define Statements

2019-01-08 11:15发布

What's the difference between using a define statement and an enum statement in C/C++ (and is there any difference when using them with either C or C++)?

For example, when should one use

enum {BUFFER = 1234}; 

over

#define BUFFER 1234   

18条回答
叛逆
2楼-- · 2019-01-08 11:51

Enum:

1. Generally used for multiple values

2. In enum there are two thing one is name and another is value of name name must be distinguished but value can be same.If we not define value then first value of enum name is 0 second value is 1,and so on, unless explicitly value are specified.

3. They may have type and compiler can type check them

4. Make debugging easy

5. We can limit scope of it up to a class.

Define:

1. When we have to define only one value

2. It generally replace one string to another string.

3. It scope is global we cannot limit its scope

Overall we have to use enum

查看更多
老娘就宠你
3楼-- · 2019-01-08 11:52

In addition to the good points listed above, you can limit the scope of enums to a class, struct or namespace. Personally, I like to have the minimum number of relevent symbols in scope at any one time which is another reason for using enums rather than #defines.

查看更多
在下西门庆
4楼-- · 2019-01-08 11:55

Another advantage of an enum over a list of defines is that compilers (gcc at least) can generate a warning when not all values are checked in a switch statement. For example:

enum {
    STATE_ONE,
    STATE_TWO,
    STATE_THREE
};

...

switch (state) {
case STATE_ONE:
    handle_state_one();
    break;
case STATE_TWO:
    handle_state_two();
    break;
};

In the previous code, the compiler is able to generate a warning that not all values of the enum are handled in the switch. If the states were done as #define's, this would not be the case.

查看更多
闹够了就滚
5楼-- · 2019-01-08 11:55

Creating an enum creates not only literals but also the type that groups these literals: This adds semantic to your code that the compiler is able to check.

Moreover, when using a debugger, you have access to the values of enum literals. This is not always the case with #define.

查看更多
狗以群分
6楼-- · 2019-01-08 11:55

While several answers above recommend to use enum for various reasons, I'd like to point out that using defines has an actual advantage when developing interfaces. You can introduce new options and you can let software use them conditionally.

For example:


    #define OPT_X1 1 /* introduced in version 1 */
    #define OPT_X2 2 /* introduced in version  2 */

Then software which can be compiled with either version it can do


    #ifdef OPT_X2
    int flags = OPT_X2;
    #else
    int flags = 0;
    #endif

While on an enumeration this isn't possible without a run-time feature detection mechanism.

查看更多
可以哭但决不认输i
7楼-- · 2019-01-08 11:56

enum can group multiple elements in one category:

enum fruits{ apple=1234, orange=12345};

while #define can only create unrelated constants:

#define apple 1234
#define orange 12345
查看更多
登录 后发表回答