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;
}

输出结果

posted on 2019-10-22 10:05  星辰之衍  阅读(196)  评论(0编辑  收藏  举报

导航