It is not possible, however, to prevent the pointer to such data from being written to. In part as a response to this problem, C++ has introduced the concept of references. A reference is a named pointer, but the address it references is set by an initialiser when it is declared and cannot be overwritten subsequently. The only different uses are in specifying formal arguments to functions, where the settin gof an actual parameter in the call is regarded as an initialiser, and in declaring a return type from a function.
int i; int& r = i; // r points to i r = 3; // r (and so i) gets value 3 in its location int j = 2; // j is a new variable r = j; // r (and so i) get the current value in j int* p = &r; // p now points to i as well int& rr = r; // rr is a reference to i as well
These show the important cases in the use of references. In particular the assignments to r after its declaration modify the contents of the location it refers to, never the address of that location.