内联函数实践:有疑惑未解决

时间:2022-04-20 21:39:28

内联函数实践:

#include <iostream>

using namespace std;

//============================================================================
//总结:
//============================================================================

void chg(int x, int y){
    int tmp = x;
    x = y;
    y = tmp;
}

inline void chg1(int x, int y){
    int tmp = x;
    x = y;
    y = tmp;
}

int main(){

    cout << "内联函数实践:" << endl;
    int a,b;
    cout << "输入a值:" << endl;
    cin>>a;
    cout << "输入b值:" << endl;
    cin>>b;

    // 传值 交换
    chg(a,b);
    cout << "1a:" << a <<  endl;
    cout << "1b:" << b <<  endl;

    // 内联 传值交换
    chg1(a,b);
    cout << "2a:" << a <<  endl;
    cout << "2b:" << b <<  endl;

    int tmp = a;
    a = b;
    b = tmp;
    cout << "3a:" << a <<  endl;
    cout << "3b:" << b <<  endl;

    cout << "end." << endl;

    return 0;
}

 

TOOD问题是: chg1函数,如果根据内联函数定义,直接将代码插入主函数体,那么按定义的执行结果 ab两个参数的值是应该交换了。

但是执行结果确实没有交换:

内联函数实践:有疑惑未解决

有哪位大神解答下这个疑惑?非常感谢!