引用作为函数参数
函数传参的实质是函数在栈区创建了(临时的)局部变量,接受传过来的数据
#include <iostream>
using namespace std;
void swap(int&,int&);
int main(int argc, const char * argv[]) {
int a =10, b =100;
cout<<"before swap --- a:"<<a <<" b:"<<b<<endl;
swap(a,b);
cout<<"after swap --- a:"<<a <<" b:"<<b<<endl;
return 0;
}
void swap(int& rx,int& ry)
{
int temp;
temp = rx;
rx = ry;
ry =temp;
}
before swap --- a:10 b:100
after swap --- a:100 b:10