C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ References

C++ References

Reference

shape Introduction

C++ got pointers from C, but it added its own capability references and references are, in a lot of important ways, much simpler than pointers. As seen in previous chapters, C++ pointers can be used to access the variables indirectly. The variables can also be accessed using references.

shape Definition

Reference can be defined as the alternate name to a variable which is already existing. It creates a copy to the original variable. A pointer to point can be changed to a different place. It can point to one pointer for a while and then start pointing to different pointer but references can't. They get their targets set when they are declared. Anything done to CPP References reflects on the target and different punctuation like star or points to is not necessary. References can be dealt directly as though it were the original object and, for most people, that makes references a lot simpler.

shape Syntax

(data_type)&   reference_variable = original_variable;

shape Example

[c] #include <iostream> using namespace std; int main () { // declaring original variables int a=15; char b = 'R'; // declare reference variables int& x = a; char& y = b; cout << "Value of a : " << a << endl; cout << "Value of a reference : " << x << endl; cout << "Value of b : " << b << endl; cout << "Value of b reference : " << y << endl; return 0; }[/c] Output [c] Value of a : 15 Value of a reference : 15 Value of b : R Value of b reference : R[/c]

shape Advantages

shape Dis-Advantages

References as function arguments

shape Description

Arguments to a function can be passed by values or references. But passing by value have some limitations.
  • When large struct or class to a function is passed by value, creates a argument copy into the function parameter. In many cases, this is not needed, as the original argument will get effected.
  • When arguments are passed by pass by value, the only way to return a value back to the caller is done through function’s return value. While sometimes it may not be suitable when function has to modify the argument passed in.
Pass by reference solves both of the above issues. References are used for sending and receiving the arguments to and from a function. To pass a variable by reference, declare the function parameters as references instead as normal variables.
void addOne(int &b) // b is a reference variable { b = b + 1; }
When the function is called, 'b' becomes a reference to the argument. Now if any changes are made to the reference are passed to the argument, since a reference to a variable is treated exactly the same as the variable itself.

shape Example

[c] #include <iostream> using namespace std; void square(int &); int main() { int i = 5; cout << "The value of given variable = " << i << endl; cout << "The address of 'i' in main() is : " << &i << endl; square(i); cout <<"The square ofgiven variable = " << i<< endl; } void square(int& ref) { ref= ref*ref; cout << "The address of 'i' in function is : " << &ref << endl; }[/c] Output [c] The value of given variable = 5 The address of 'i' in main() is : 0x23fe3c The address of 'i' in function is : 0x23fe3c The square ofgiven variable = 25[/c]

Summary

shape Key Points

  • Reference is indirect way of accessing a variable which is very secured.
  • CPP References can be applied when working with functions.
  • Once a reference is created, it cannot be later made to refer another object.
  • CPP References are less powerful compared with pointers.

shape Programming Tips

  • References cannot be reinitialized.
  • Null value cannot be stored in reference variable because it should hold any of the value all time.