参照(References)


参照(References)
参照は、あるオブジェクトの別の名前を指し、彼の住所は元のオブジェクトと同じです.参照は主に関数のパラメータと戻り値を表すために使用され、特に演算子の再ロードのために使用されます.
参照の基本概念をコードで説明します.
void referencesTest(){
       int a = 0;
       int& b = a;
       cout<<&a<<""<       cout<<&b<<""<       b++;
       cout<<"/n"<<&a<<""<       cout<<&b<<""<}
出力は次のとおりです.
       0012FF24       0
0012FF24
       0
 
0012FF24
       1
0012FF24
       1
 int a = 12;  int &ra = a;     int &rr1 = ra;//OK!// int &&rr2 = ra;//error!  cout << rr1 <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!