Reference Parameters
Reference parameters allow us to pass a parameter by its reference rather than its value. Reference values are the norm in C++.
- Pass a parameter to a function that allows the function to produce a side effect on it
void swap(int& x, int& y) {
const int temp = x;
x = y;
y = temp;
}
- const references: pass readonly reference without copying value (see Constants)
References
// Reasonable use for a reference
Employee & e = employeeList[id];
cout << e.getName() << endl;
// Useless reference
int x = 13;
int& y = x; // y is an aliases to x now
y = 36; // also changes x