不用第三方参数,交换两个参数的值

来源:互联网 发布:水果淘宝 编辑:程序博客网 时间:2024/05/21 22:41

#include <iostream>using namespace std;void Swap1(int &a, int &b) { a = a + b - (b = a); }int main(){int a = 4, b = 5;cout << "before swap: ";cout << "a = " << a << ", b = " << b << endl;Swap1(a, b);cout << "after swap: ";cout << "a = " << a << ", b = " << b << endl;return 0;}



gcc 4.1.2运行,三个自定义函数都能交换两个变量的值。

VS 2008下面,Swap1()函数被优化了,没有正确交换两个变量的值,表示不懂。求解释~

原创粉丝点击