void swap(int &a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
std::cout << " swap a = "<< a << std::endl;
}
int main()
{
int a = 1, b = 2;
swap(a,b);
std::cout << " main a = "<< a << std::endl;
}
结果为 swap a = 2
main a = 2
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
std::cout << " swap a = "<< a << std::endl;
}
int main()
{
int a = 1, b = 2;
swap(a,b);
std::cout << " main a = "<< a << std::endl;
}
结果为swap a = 2
main a = 1
形参不带取地址符是赋值了临时新变量
带了取地址符是直接用之前的变量