References and Pointers

Defining References

describes how to define references and pointers and how to use them as parameters and/or return values of functions. In this context, passing by reference and read-only access to arguments are introduced.

Example

float x = 10.7,  &rx = x;

Sample program

// Ref1.cpp
// Demonstrates the definition and use of references.
// ---------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
   
float x = 10.7F;                           // Global
   
int main()
{
   float  &rx = x;          // Local reference to x
// double &ref = x;         // Error: different type!
   
   rx *= 2;
   
   cout << "   x = " <<  x << endl    //  x = 21.4
        << "  rx = " << rx << endl;   // rx = 21.4
   const float& cref = x;    // Read-only reference
   cout << "cref = " <<  cref << endl;   // ok!
// ++cref;                   // Error: read-only!
   const string str = "I am a constant string!";
// str = "That doesn't work!";  // Error: str constant!
// string& text = str;          // Error: str constant!
   const string& text = str;    // ok!
   cout << text << endl;        // ok! Just reading.
   return 0;
} 
A reference is another name, or alias, for an object that already exists. Defining a reference does not occupy additional memory. Any operations defined for the reference are performed with the object to which it refers. References are particularly useful as parameters and return values of functions.

Definition

The ampersand character, &, is used to define a reference. Given that T is a type, T& denotes a reference to T.
Example :
float x = 10.7;
float& rx = x;   // or:  float &rx = x; 
rx is thus a different way of expressing the variable x and belongs to the type "reference to float". Operations with rx, such as
Example:
--rx;            // equivalent to  --x; 
will automatically affect the variable x. The & character, which indicates a reference, only occurs in declarations and is not related to the address operator &! The address operator returns the address of an object. If you apply this operator to a reference, it returns the address of the referenced object.
Example:
&rx    // Address of x, thus is equal to &x 
A reference must be initialized when it is declared, and cannot be modified subsequently. In other words, you cannot use the reference to address a different variable at a later stage.

Read-Only References

A reference that addresses a constant object must be a constant itself, that is, it must be defined using the const keyword to avoid modifying the object by reference. However, it is conversely possible to use a reference to a constant to address a non-constant object.
Example:
int a;    const int& cref = a;  // ok! 
The reference cref can be used for read-only access to the variable a, and is said to be a read-only identifier.
A read-only identifier can be initialized by a constant, in contrast to a normal reference:
Example:
const double& pi = 3.1415927; 
Since the constant does not take up any memory space, the compiler creates a temporary object which is then referenced.

Post a Comment

0 Comments