C++学习笔记之 内联函数

内联函数

宏函数的缺点

  1. 缺陷1:要加小括号,保证运算完整

  2. 缺陷2:即使加了括号,有时也与预期不符

内联函数

本质也是一个普通函数,在适当时候会替换源码

注意

  • 不能存在循环语句
  • 不能存在过多条件判断语句
  • 函数体不能过于庞大
  • 不能对函数进行取址操作

语法

inline 函数()
{
    // ...
}

#include <iostream>
#include <cstdlib>
#include <cstdio>

inline void jiaohuan(int &a,int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a = 1, b = 2;
    printf("a = %d, b = %d
",a,b);
    jiaohuan(a,b);
    printf("a = %d, b = %d
",a,b);

    return EXIT_SUCCESS;
}
a = 1, b = 2
a = 2, b = 1
原文地址:https://www.cnblogs.com/zhujiangyu/p/13938911.html