inline内联函数

C++中的const常量能够替代宏常数定义,如:ios

const int A = 3; è #define A 3函数

C++中是否有解决方案替代宏代码片断呢?(替代宏代码片断就能够避免宏的反作用!)spa

 

C++中推荐使用内联函数替代宏代码片断code

 

C++中使用inline关键字声明内联函数blog

 

 

内联函数声明时inline关键字必须和函数定义结合在一块儿,不然编译器会直接忽略内联请求。编译器

 

带参数的宏和函数调用io

 

#include <iostream>
using namespace std;

//带参数的宏
#define MYFUNC(a,b) ((a)<(b)?(a):(b))

inline int myfunc(int a, int b)
{
    return a < b ? a : b;
}

int main(void)
{
    int a = 1;
    int b = 3;
    
    //通常状况不要使用++,--作函数参数
    int c = myfunc(++a,b); //a=2,b=3,c=2
    //int c = MYFUNC(++a,b);  //a=3,b=3,c=3
                        //宏替换并展开 ((++a)<(b)?(++a):(b))
    
    printf("a=%d\n",a);
    printf("b=%d\n",b);
    printf("c=%d\n",c);
    
    cout<<"hello..."<<endl;
    
    return 0;    
}
相关文章
相关标签/搜索