cppTest-2.1:内联函数与宏定义的比较
2013-12-28 11:19
302 查看
/** *cppTest-2.1:内联函数与宏定义的比较 * *1.内联函数在运行时可调试,而宏定义不可以; *2.编译器会对内联函数的参数类型做安全检查或自动类型转换(同普通函数),而宏定义则不会; *3.内联函数可以访问类的成员变量,宏定义则不能; *4.在类中声明同时定义的成员函数,自动转化为内联函数。 * *author 炜sama */ #include<iostream.h> #define Fun(x) ((x>5)&&(x<10)? (x):0) inline double max(double x,double y); inline int fun(int x); void main(){ double a,b; cout<<"Enter two real:"; cin>>a>>b; cout<<max(a,b)<<endl; int c = 7; int d = Fun(++c);//注意:这个不等价于下面的fun(++c)!因为宏定义是简单的文本替换,即等价于d=( (++c>5)&&(++c<10)? (++c):0 ); ! cout<<"c:"<<c<<",d:"<<d<<endl; int e = 7; int f = fun(++e); cout<<"e:"<<e<<",f:"<<f<<endl; } double max(double x,double y){ int v=0; switch(v){ default:cout<<"switch"<<endl; } return x>y?x:y; } inline int fun(int x){ return ((x > 5) && (x < 10)) ? x : 0; }
相关文章推荐
- 【CF 应用开发大赛】IT Share(IT分享网)
- 关于指针的一些事情
- IT职场英语,必须学会
- 小白兔求职篇 搞笑
- it人员要看饮食保健养生谚语
- C/C++数据对齐详细解析
- C++中引用的使用总结
- C与C++之间相互调用实例方法讲解
- C++中引用(&)的用法与应用实例分析
- 解析C++ 浮点数的格式化输出
- 深入分析C++中几个最不常用的关键字
- c++中inline的用法分析
- 深入解析C++ Data Member内存布局
- 从汇编看c++中默认构造函数的使用分析
- 关于C++中的友元函数的一些总结
- C++的sstream标准库详细介绍
- 基于C++自动化编译工具的使用详解
- 浅谈C++中的string 类型占几个字节
- C/C++ 宏详细解析
- 深入分析C++中两个大数相乘结果不正确的问题