References in C++

A reference variable can be compared with synonym in C++ programming language. It means that a reference variable is simply another name for already existing object. References can be mistaken for pointers. It’s not correct because references are less powerful than pointers. But references are safer than pointers. There are some important differences between C++ references and pointers:

Difference between Pointers and References

  1. References must be initialized once they are declared. So, a reference can’t be NULL
  2. Once a reference is declared and initialized there is no possibility to make reference variable to reference another object.
  3. Pointers can point to no-where (NULL) but a reference always refers to some object.
  4. We cannot take the address of the reference as we can do it with pointers.
  5. You cannot do reference arithmetic like we can do with pointers.

References syntax

The declaration syntax is:

type& Name = referencedObject;

where type is any data type and Name is identifier of the reference variable. Once you declared a reference variable – you must initialize it. Here are the examples of creating and using reference variable:

int a = 10; //integer variable is created
int& referenceToA = a;//a reference to variable 'a' is created
cout << "Value of a is " << a << endl;
cout << "Value referenced by \"referenceToA\" " << referenceToA << endl;

The output is:
Value of a is 10
Value referenced by “referenceToA” 10

The line:

int& referenceToA = a;

can be read as : “referenceToA variable is an integer reference to variable a”.

Now identifiers “a” and “referenceToA” are identifiers of the same memory location. This means that there is no difference in use of both identifiers. Here is an example of changing a variable using reference:

//decrease the value referenced by referenceToA
referenceToA = 2;
cout << "Value of a is " << a << endl;

The output for this code is:

Value of a is 2

Reference variable is a kind of variable. That’s why the same rules for the variable’s scope, modifiers types and storage classes are applicable to the reference variable.

Reference to Function

You can create references to the functions too. For example, you have a function that returns an integer and takes a double as a parameter:

int foo(double i)
{
	return 2;
}

To create a reference to a function you have to use the following syntax:

type (&identifier) (parameter list) = referenced function;

Where type is return type of the function,

identifier – is the name of reference
parameter list – parameter list of the referenced function.

A reference to the function foo is defined in the following way:

int(&referenceToFunction)(double) = foo;

Now there is a possibility to call function foo by using a reference:

referenceToFunction(3.0);

Reference as a Parameter of a Function

You can use a reference as a parameter of a function. Using references as parameters of the function is similar to using pointers as parameters of the function. But sometimes it is safer than using pointers because of the difference between pointers and references. Take a look on example of a function that uses a reference as a parameter:

void opposite(int& a)
{
	//change a to oposite
	a *= (-1);
}

The parameter of this function is a reference: int& a

In the case we call this function with a variable; the value of the variable will be changed to negative inside the function:

int k = 7;
cout << "k before function call " << k << endl;
opposite(k);
cout << "k after function call " << k << endl;

The value of k will be changed because is passed to a function by reference:

k before function call 7
k after function call -7

Reference as a Return Type

Reference as a return type is often used when you need to return a value from parameters of the function or return a value of global variable. For example, we have an array declared as a global variable as shown below:

int arr[] = { 2, 6, 34, 2, 0, -4, 2 };

We can create a function that will return array’s element corresponding to the index. If you want to use a reference as a return type of the function you will need to specify the return type of function as a reference:

int& getElement(int index)
{
	return arr[index];
}

This function will return the reference to the element with index “index”

This is an example of calling a function with reference return type:

cout << "arr[3] = " << arr[3] << endl;
int& ref =  getElement(3);
ref = 0;
cout << "arr[3] = " << arr[3] << endl;

Once you change ref, you will change the element with index 3 in the array:

arr[3] = 2
arr[3] = 0

Because of the fact that the return type of the function is a reference – function call can be used as a left value and the previous operation can be done in the following way:

cout << "arr[3] = " << arr[3] << endl;
getElement(3) = 0;
cout << "arr[3] = " << arr[3] << endl;

 

Translate ยป