int & const ref1 = age; //不能修改指向,可以赋值ref1间接修改age的值
ref1 =30;
int const &ref1 = age; //什么都不能
const int &ref = 30;
int sum(int &v1, int &v2) {
return v1 + v2;
}
int main() {
// 非const实参
int a = 10;
int b = 20;
sum(a, b);
// const实参
const int c = 10;
const int d = 20;
sum(c, d);
sum(10 ,20); //这里会报错,函数声明必须加const
/*
int sum(const int &v1,const int &v2)
*/
}
int age = 10;
const long &rAge = age; //写int写long结果不一样
age = 30;
cout << "age is" << age << endl;
cout << "rAge is" << rAge << endl;
原文:https://www.cnblogs.com/sec875/p/12261694.html