【发布时间】:2010-09-16 11:23:48
【问题描述】:
在下面的代码中,amp_swap() 和 star_swap() 似乎都在做同样的事情。那么为什么有人会更喜欢使用其中一种呢?哪一个是首选符号,为什么?还是只是口味问题?
#include <iostream>
using namespace std;
void amp_swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void star_swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a = 10, b = 20;
cout << "Using amp_swap(): " << endl;
amp_swap(a, b);
cout << "a = " << a << ", b = " << b << endl;
cout << "Using star_swap(): " << endl;
star_swap(&a, &b);
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
感谢您的宝贵时间!
另见
Difference between pointer variable and reference variable in C++
【问题讨论】: