标签:
int ival = 1024; int& refVal = ival;// ok:refVal refer to ival int& refVal2; //error: a reference must be innitialized int& refVal3 =10; //error: cannot take type"int" to "int&"引用就是一个别名!!!
C++允许在一个定义行中定义多个引用。但是,必须在每个引用标识符前添加“&”符号:
const引用是指向const类型的引用!
const int ival = 1024; const int& refVal = ival; // ok: both reference and object are const int& refVal2 = ival; // erroe: non const reference to a const object
经过const限定后,我们就可以初始化为右值,如字面值常量:
int ival = 42; // legal for const reference only const int& refVal = 42; const int& refVal = ival +1;需要注意的是,同样的初始化方法对于非const引用是不合法的,并且会导致编译的错误。
标签:
原文地址:http://blog.csdn.net/shenziheng1/article/details/51352820