下面的实例使用了 int 和 double 引用:
实例
#include<iostream>usingnamespacestd; intmain(){ // 声明简单的变量 int i; doubled; // 声明引用变量 int& r = i; double& s = d; i = 5; cout << "Value of i : " << i << endl; cout << "Value of i reference : " << r << endl; d = 11.7; cout << "Value of d : " << d << endl; cout << "Value of d reference : " << s << endl; return0;}
当上面的代码被编译和执行时,它会产生下列结果:
Value of i :5
Value of i reference :5
Value of d :11.7
Value of d reference :11.7