A reference is an alternative name for an existing variable. A reference is declared by specifying the variable type followed by &:
int var = 1;
int& ref = var;Reading and writing the reference reads and writes the original variable:
ref = 2;
std::cout << var << "\n"; // 2References must be initialised when declared:
int& ref; // ERROR.References cannot rebound to another variable:
int a = 1;
int& ref = a;
int b = 2;
ref = b; // a is now 2, ref still refers to a.A const reference can read but not modify the variable it refers to:
int var = 1;
const int& ref = var;
ref = 2; // ERROR.References are most often used as function parameters so the function can modify the caller's variable, or to avoid copying large objects:
void increment(int& n) { n++; }
void print(const std::string& s) { std::cout << s; }
int count = 0;
increment(count); // count is now 1.Note
Prefer references over pointers where possible. A reference can never be null or uninitialised, so it does not need to be checked before use.
Dan