优势与主场迎战的#define常量的缺点是什么? [英] Advantage and disadvantages of #define vs. constants?

查看:186
本文介绍了优势与主场迎战的#define常量的缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能指出使用的#define 对常量的优点和缺点?我的大部分工作是在C和Objective-C完成的。

Can someone point out the advantages and disadvantages of using #define versus constants? Most of my work is done in C and Objective-C.

推荐答案

常量允许你指定一个数据类型,这是(通常情况下)的优势。宏是更加灵活,因此可以让你陷入更麻烦,如果你不小心。

Constants allow you to specify a datatype, which is (usually) an advantage. Macros are much more flexible, and therefore can get you into much more trouble if you're not careful.

最佳做法是使用常量尽可能多地,并用#定义只有当你真正需要一个宏,而不仅仅是一个名为文字值。

Best practice is to use constants as much as possible, and use #define only when you really need a macro, not just a named literal value.

这篇关于优势与主场迎战的#define常量的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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