References in C++

Rishabh Agarwal
3 min readMar 5, 2023

Looking for more C++ related resources? Explore other blog posts in the series —

  1. Virtual Functions in C++
  2. Constructors in C++
  3. Constant Variables in C++
  4. Inline Functions in C++
  5. Default Parameters in C++
  6. Function Overloading in C++
Photo by Jørgen Håland on Unsplash

References in C++ are a way to create aliases or synonyms for variables. A variable can be declared as a reference variable by using ampersand (&) symbol in the declaration.

int i = 5; // i is a variable
int& j = i; // j is a reference variable for i

i = 7;
assert(j==7); // returns true

j = 10;
assert(i==10); // returns true

Reference variables points to the same memory location as the original variable. Thus changing one variable results in the value of the other variable also getting changed.

Pitfalls in Reference

References must always be initialised at the time of declaration. Thus un-initialised reference variables can not be created.

int p = 5;
int &i; // Wrong declaration since the variable is not initialised
int &j = p; // Correct declaration!

References can not be initialised to constants or expressions. This pitfall can however be avoided using the constant variables as shown in the example below.

int& j = 5; // Incorrect
const int& k = 5; // Correct

int& p = 2*k; // Incorrect
const int& q = 2*k; // Correct

Use of const instructs compiler to allocate memory for constants and expressions. This memory is then referred to by the reference variable.

Call By Reference

Consider the following C++ function and its associated usage from inside the main function.

void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int p = 5, q = 7;
swap(p, q);
cout<<"p = "<<p<<", q = "<<q<<endl;
}

We know that the function above does not achieve what it tries to. This is because the function uses call-by-value. In call by value, actual parameters are copied to the formal parameters. Thus, any changes to the formal parameters are not reflected to the formal parameters.

This problem can be easily solved using reference variables. Consider this second defintion of a similar function.

void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

There is just a slight difference between this function and the function we saw before. The parameters here are reference variables. Since the changes are made to the reference variables, the actual variables are swapped. This is known as Call-By-Reference.

Some functions may also take reference variable as constants. This is done to indicate that passed in variables are not changed.

Return By Reference

Similar to call-by-reference, we have return-by-reference as well. This is how it can be used.

int& func(int& x) {
cout<<"X = "<<x<<endl;
return x;
}

Note: Never return a local variable by reference.

It is a general practice to pass built-in-type by values and user-defined types by reference.

References Vs Pointers

Both pointer and reference refers to an address. While pointer can be NULL, references can never be NULL. Pointers can point to different variables as different times but a reference is fixed.

int a, c, &b = a;

&b = c; // Incorrect

While you can perform operations on address stored in a pointer, similar thing can not be done for refernces.

That is it for today guys. Stay tuned for more articles on C++. Do not forget to check out my other articles on C++. Happy Learning!

--

--

Rishabh Agarwal

Software Engineer | Loves to write about Programming, Technology, and Mathematics!