//1、引用是给变量取别名,定义的时候必须初始化,且指向一个确定的内存空间; //2、引用实质是指针常量,值可以改变,地址不可改变,即不可重复赋值
#include <iostream>
using namespace std;//1、引用是给变量取别名,定义的时候必须初始化,且指向一个确定的内存空间;
//2、引用实质是指针常量,值可以改变,地址不可改变,即不可重复赋值//引用做形参
void swap(int& pa ,int& pb)
{int tmp;tmp = pa;pa = pb;pb = tmp;
}
//引用做返回值
double sz[] = {1.0,2.0,3.0,45.3,493.6,55.2};
double& Ret(int i)
{double& tmp = sz[i];return tmp;
}int main()
{int a = 10;int b = 20;int& ya = a;int& yb = b;cout << "引用前 a="<<a << " b=" <<b<< endl;ya = 100;yb = 200;cout << "引用后1: a="<<a << " b=" <<b<< endl;swap(a,b);cout << "交换后: a="<<a << " b=" <<b<< endl;Ret(1) = 1000.0;cout << "sz[1]="<<Ret(1)<< endl;return 0;
}