c++的引用
#include <iostream>
using namespace std;
//引用一
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
//引用二:作为返回值,作为左值
double vals[] = {40.2,23.0,394,34.3,4.6};
double &setVal(int i)
{
double &ref = vals[i];
return ref;
}
int main()
{
int a = 3;
int b = 4;
cout << "交换之前:";
cout << "a=" << a << " b="<< b << endl;
swap(a,b);
cout << "交换之后:";
cout << "a=" << a << " b="<< b << endl;
cout << "Hello World!=================" << endl;
setVal(3) = 66;
cout << vals[3] << endl;
for(int j = 0;j <= 4;j++ ){
cout << "改变后的值:" << vals[j]<< endl;
}
return 0;
}