effective C++ Item 2: Prefer consts, enums, and inlines to #defines
来源:互联网 发布:windows程序实例 编辑:程序博客网 时间:2024/04/29 10:22
前面一款基本上就是介绍性的内容,这一节进入主题。
条款二:尽量以const,enum,inline代替#define
1:使用const代替#define定义常量。
如果你定义圆周率如下:
#define PI 3.1415926
那么意味着预处理器将会把程序中出现PI的地方都替换为value(3.1415926),这样的话在编译阶段PI不会进入到符号表中,调试的时候如果此处出现错误,只会提示说 3.1415926怎么怎么样,而不是PI怎么样。对于大程序来说,无疑给程序的调试带来很大困难,因为你有时候根本想不起来这个值是怎么来的。
推荐做法:以
const double PI = 3.1415926
代替。
2:使用enum代替#define
当你编译阶段需要一个常量值时,const就无能为力了。这时你需要enum。
比如你定义一个数组:
int scores[numTurns];
编译器坚持要知道numTurns的大小。这时候可以使用enum:
enum{numTurns=5};
enum的行为很像#define,区别是enum可以取地址,而#define则不行。与const的区别就是:const数据有类型,而enum的隐含数据类型是整数,其最大值有限,且不能表示浮点数。
3:使用inline代替#define
用#define实现宏,作用和函数一样,却没有函数调用时的开销,但是宏定义查查引起不必要的错误,比较经典的就是两数比较:
#define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))
尽管你每个变量都加了括号,但还是有错误发生,那就是传入++a,b,当a>b时,会发生a加两次的情况,这个都懂的。你可以用下面的语句代替:
template <typename T>inline void callWithMax(const T& a,const T& b){ f(a>b ? a : b);}
inline取得了和宏定义一样的高效,并在其基础上增加了安全性。
但#define还是有其用武之地的,看看MFC类库就明白了。
- Effective C++ Item 2:Prefer constS, enumS, and inlineS to #defineS
- effective C++ Item 2: Prefer consts, enums, and inlines to #defines
- Effective C++读书笔记(3)-Item 2: Prefer consts, enums, and inlines to #defines
- Item 02 : Prefer consts, enums, and inlines to #defines
- Item 02: Prefer consts, enums, and inlines to #defines
- Effective C++ 2. Prefer consts, enums, and inlines to #defines
- Effective C++ (3rd Ed) 读书笔记(一)Item 2: Prefer constS, enumS, and inlineS to #defineS
- Effective C++读书笔记——(一)Prefer consts, enums, and inlines to #defines
- 《Effctive C++》读书笔记--(02)Prefer consts,enums,and inlines to #defines
- [翻译] Effective C++, 3rd Edition, Item 2: 用 consts, enums 和 inlines 取代 #defines
- 条款02:尽量以const, enum, inline替换#define(Prefer consts,enums, and inlines to #defines)
- Effective C++----3rd Edition, Item 2:用consts,enums和inlines取代#define
- 笔记:Perfer consts,enums,and inlines to #define
- Effective C++,rule 2,Prefer const,enum and inlines to #define
- Prefer const, enum ,inlines to #defines
- Effective Objective-C 2.0: Item 42: Prefer GCD to performSelector and Friends
- <Effective Mordern C++>笔记:Item 8:prefer nullptr to 0 and NULL.
- effective c++ 学习新得之 条款2 prefer const,enum, inline to #defines
- 后台的SQL语句插入Blob字段数据
- UINavigationController 设置左右按钮
- jsontools实现java对象与json值之间互相转换
- 《穷爸爸、富爸爸》读后感
- SQL Server 2005里的Image处理
- effective C++ Item 2: Prefer consts, enums, and inlines to #defines
- Some Tips In Object C
- 【C++基础学习】C++引用参数与指针参数
- 数字的全排列 问题
- 开个头
- Windows与虚拟机Linux之间的访问问题
- eclipse平台编码总结(不断添加)
- 一位软件工程师的6年总结
- Eclipse开发Javascript环境配置(含EXTJs配置) 转载