A reference aliasing a constant object must be a constant itself.
It must be defined using the const keyword to avoid modifying the object by reference.
int a; const int& cref = a; // ok!
The reference cref can be used for read-only access to the variable a, and is said to be a read-only identifier.
A read-only identifier can be initialized by a constant, in contrast to a normal reference:
const double& pi = 3.1415927;
// Demonstrates the definition and use of references. #include <iostream> #include <string> using namespace std; float x = 1.7F; int main() //ww w . j a va2 s . c om { float &rx = x; // Local reference to x rx *= 2; const float& cref = x; // Read-only reference cout << "cref = " << cref << endl; return 0; }