1、引用(&)是标识符的别名
2、定义一个引用时,必须同时对它进行初始化,使它指向一个已存在的对象
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i,j;
int &ri=i;//定义int引用ri,并初始化为变量i的引用
j=10;
ri=j;//相当于i=j
cout<<ri;
system("Pause");
return 0;
}
3、一旦一个引用被初始化后,不得再指向其他对象
4、引用可以作为形参,用于实现参数的双向传递
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x=1,y=2;
swap(x,y);
cout<<x<<ends<<y<<endl;
system("Pause");
return 0;
}