C++引用与传参
# include <iostream>
using namespace std;
void Swap(int *pa, int *pb)
{
int t = *pa;
*pa = *pb;
*pb = t;
cout << "*pa = " << *pa << endl;
}
void Swap(int &a, int &b) {
int tmp;
tmp = a;
a = b;
b = tmp;
cout << "tmp = " << tmp << endl;
}
int main()
{
int a = 3, b = 5;
Swap(&a, &b); /*执行第一个*/
Swap(a, b); /*执行第二个*/
printf("a = %d""; b = %d\n", a, b);
cout << "a = " << a << "; b = " << b << endl;
return 0;
}
输出结果
本文版权归作者和博客园共有,欢迎转载,转载请注明出处和链接来源。