ENUM和定义报表之间的差异 [英] Difference between Enum and Define Statements

查看:94
本文介绍了ENUM和定义报表之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(与C或C ++使用的时候,并没有任何区别)有什么用define语句,并在C / C ++枚举声明之间的区别

例如,当一个应用

 枚举{BUFFER = 1234};

 的#define BUFFER 1234


解决方案

枚举定义语法元素。

的#define 是pre-preprocessor指令,执行的的编译器看到code和因此不是C本身的语言元素。

一般枚举是preferred,因为他们是类型安全,更容易被发现。定义更难找到并可以有复杂的行为,比如一件code可以重新定义一个的#define 由另一个制作。这可能是很难追查。

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   

解决方案

enum defines a syntactical element.

#define is a pre-preprocessor directive, executed before the compiler sees the code, and therefore is not a language element of C itself.

Generally enums are preferred as they are type-safe and more easily discoverable. Defines are harder to locate and can have complex behavior, for example one piece of code can redefine a #define made by another. This can be hard to track down.

这篇关于ENUM和定义报表之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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