January 13, 2021

C++ References

The concepts of references¬† is one of the key concepts in programming. This is regardless of the programming language. So ensure you understand it since is quite easy and clear. Also since it relates to pointer, it’s better you go over C++ Pointers first before you continue.

  1. What are References
  2. Creating References in C++
  3. Passing Parameters by Reference
  4. References as Function Return Value

 

1. What are References?

A reference variable or just reference, is a variable that refers to an existing variable. Once you create a reference, then you can refer to to variable using that reference.

Is this not the same as pointers? Well, no. Here are the differences

References vs Pointers

ReferencesPointers
You cannot have NULL referencesYou can have NULL pointers
Once initialized, a reference cannot be changed to reference another objectPointers can be modified to point to another object at any time
A reference must be initialized at creation timePointers can be initialized at any time

 

2. Creating References in C++

A reference can be thought of an an alias of a variable name. So in the code below, we have a variable x. So we create a reference variable r to refer to x.

// declare a variables, x
int x = 34;

// create a reference rx
int& rx = x 

The program below shows the use of references.

int main () {
	   // declare two normal variables
	   int    a;
	   double b;
	 
	   // declare two reference variables
	   int&    ra = a;
	   double& rb = b;
	   
	   a = 100;
	   cout << "Value of a : " << a << endl;
	   cout << "Value of ra reference : " << ra  << endl;
	 
	   b = 70.25;
	   cout << "b has a value of : " << b << endl;
	   cout << "The reference has a value of : " << rb  << endl;
	   
	   return 0;
}

The output of this program is:

Value of a : 100
Value of ra reference : 100
b has a value of : 70.25
The reference has a value of : 70.25

So you can see that references, a like another name for the variable is references.

 

3. Passing Parameter by Reference

You may wonder. Why do we need references? Well in many cases, passing parameter by reference would lead to optimized memory utilization. Passing parameter by reference, means that the formal parameters are reference variables. So they are references to the actual parameters. Example is given the the following code.

int main () {
	   // declare two normal variables
	   int x = 10;
	   int y = 20;
	   
	   cout << "Value of x and y before swap" << endl;
	   cout << "x before swap : " << x << endl;
	   cout << "y before swap : " << y  << endl;
	   
	   // call swap() to swap the two numbers
	   swap(x, y);

	   cout << "Value of x and y after swap" << endl;
	   cout << "x after swap : " << x << endl;
	   cout << "y after swap : " << y  << endl;

	   return 0;
}

The program would give the following output

Value of x and y before swap
x before swap : 10
y before swap : 20
Value of x and y after swap
x after swap : 20
y after swap : 10

 

4. Reference as Function Return Value

A function have be made to return a reference. When a function returns a reference, then it kind of returns a pointer to its return value

#include <iostream>

using namespace std;

double scores[] = { 67.5, 98.8, 75.7};

double& setValues( int i ) {
   return scores[i];   // return a reference to the ith element
}

// main function
int main () {

   cout << "Scores before change" << endl;
   for ( int i = 0; i < 3; i++ ) {
      cout << "scores[" << i << "] = ";
      cout << scores[i] << endl;
   }

   setValues(0) = 100.0; // change 1st element
   setValues(2) = 100.0;  // change 3rd element

   cout << "Scores after change" << endl;
   for ( int i = 0; i < 3; i++ ) {
      cout << "scores[" << i << "] = ";
      cout << scores[i] << endl;
   }
   return 0;
}

The program above would produce the following output.

Scores before change
scores[0] = 67.5
scores[1] = 98.8
scores[2] = 75.7
Scores after change
scores[0] = 100
scores[1] = 98.8
scores[2] = 100

Leave a Reply

Your email address will not be published. Required fields are marked *