您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息