#include <iostream>
using namespace std;
void swap_1(int a ,int b)//fail 传值参数
{
int temp=a;
a=b;
b=temp;
}
void swap_2(int* a,int* b)//指针参数
{
int temp=*a;
*a=*b;
*b=temp;
}
void swap_3(int& a,int &b)//引用参数
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int a=1,b=2,c=3,d=4,e=5,f=6;
swap_1(a,b);
swap_2(&c,&d);//传地址
swap_3(e,f);
cout<<a<<b<<c<<d<<e<<f<<endl;
}
输出:124365,
也即:
1、2位置没变(swap_1()函数不起作用),
3、4和5、6位置变了,(swap_2()函数起作用、swap_3()函数起作用)