[C++] Inline function ① ( Inline function introduction | Inline function syntax )





1. Introduction of inline functions




1. Introduction of inline functions


"Inline function" is a special function in C++ language, its purpose is to improve the execution efficiency of the program;


In C++, define a constant

const int a = 10

Can replace macro definition in C language

#define a 10

Use constants instead of macro definitions;


There are two types of macro definitions, one is macro constants , and the other is macro code fragments;

  • Use constants instead of macro constants in C++;
  • In C++, the inline function is used to replace the macro code fragment;

In C++, developers are advised to use "inline function" instead of "macro code fragment" in C language;


Example macro code snippet:

// 宏代码片段
#define FUN(a, b) ((a) < (b) ? (a) : (b))

Inline function example: The following inline function can replace the above macro code fragment, and the functions of the two are basically the same;

// 内联函数
inline int fun(int a, int b)
{
    
    
    return a < b ? a : b;
}

2. Code example - macro code fragment and inline function


In the following code, the macro code fragment FUN(a, b) and the inline function inline int fun(int a, int b) are respectively defined. The functions implemented by the two are basically the same, and both can realize the acquisition of a and b. the smaller of the values ​​;


Code example - macro snippet and inline function:

// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;

// 宏代码片段 : 获取 a 和 b 中较小的值
#define FUN(a, b) ((a) < (b) ? (a) : (b))

// 内联函数 : 获取 a 和 b 中较小的值
inline int fun(int a, int b)
{
    
    
    return a < b ? a : b;
}

int main() {
    
    

    // 控制台暂停
    system("pause");

    return 0;
}




2. Inline function syntax




1. Inline function syntax description


The C++ inline function syntax is as follows:

inline 函数声明 {
    
      
    // 函数体
}

A function declaration consists of:

  • Function name
  • parameter list
  • return type

Add the inline keyword before the function declaration, that is, the ordinary function, to declare the ordinary function as an inline function;


The call of the inline function is the same as that of the ordinary function, it can be called directly; there is only a difference when compiling, but there is no difference when using it;


2. Code example - basic syntax of inline function


In the following code, the inline function just adds the inline keyword before the int fun(int a, int b) function declaration;

When calling this function, just use the function name fun to call it, which is the same as the normal function call method;


Code example:

// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;

// 宏代码片段 : 获取 a 和 b 中较小的值
#define FUN(a, b) ((a) < (b) ? (a) : (b))

// 内联函数 : 获取 a 和 b 中较小的值
inline int fun(int a, int b)
{
    
    
    return a < b ? a : b;
}

int main() {
    
    

    // 调用内联函数
    int a = fun(1, 2);
    // 打印内联函数调用结果
    printf("a = %d\n", a);

    // 控制台暂停
    system("pause");

    return 0;
}

Results of the :

a = 1
请按任意键继续. . .

insert image description here

Supongo que te gusta

Origin blog.csdn.net/han1202012/article/details/132632231
Recomendado
Clasificación