引用(References)
int a = 12;
int &ra = a;
int &rr1 = ra; // OK!
// int &&rr2 = ra; // error!
cout << rr1 <<endl;
rr1 = 3;
cout << a << " " << ra << " " << rr1 << endl;
//输出结果rr1: 12
// a b c: 3 3 3
// int &*pri; // error! //不允许对指针的引用
// int &ar[3]; // error! //不允许对数组的引用
References can't be const or volatile, because aliases can't be const or volatile, though a reference can refer to an entity that is const or volatile. An attempt to declare a reference const or volatile directly is an error:
int &const cri = a; // should be an error . . .
const int &rci = a; // OK
Strangely, it's not illegal to apply a const or volatile qualifier to a type name that is of reference type. Rather than cause an error, in this case the qualifier is ignored:
typedef int *PI; typedef int &RI; const PI p = 0; // const pointer const RI r = a; // just a reference!
There are no null references, and there are no references to void:
C *p = 0; // a null pointer C &rC = *p; // undefined behavior extern void &rv; // error!