#include <iostream>
using namespace std;
class A{
public:
A(int &a):y(a){ //y must be initialized in the initializer list
}
private:
int &y;
};
const int size=50;
int array[size];
int &f(const int i){ //functions can return references,but they'd better
//refer to non-local variables!
return array[i];
}
void f2(const int &i){ //若为void f2(int &i),28行代码错误
cout<<i<<endl;
}
int main(){
int value=f(14); //实际上:把array[14]的值给了value
f(3)=34; // f(3)的返回值所代表的变量等于34
int i=3;
f2(i*3); //相当于有一个临时变量存i*3
return 0;
}
reference
最新推荐文章于 2021-02-05 18:28:17 发布