Learn C++ - C++ Reference Variables






C++ reference is a name that acts as an alternative name for a previously defined variable.

For example, if you make Bob a reference to the Robert variable, you can use Bob and Robert interchangeably.

The main use for a reference variable is as a formal argument to a function.

If you use a reference as an argument, the function works with the original data instead of with a copy.

References provide a convenient alternative to pointers for processing large structures with a function.

Creating a Reference Variable

C and C++ use the & symbol to indicate the address of a variable.

C++ uses & symbol to declare references.

For example, to make robert an alternative name for the variable rats, you could do the following:

int bob; 
int & robert = bob;    // makes robert an alias for bob 

In this context, & is not the address operator.

Instead, it serves as part of the type identifier.

int & means reference-to-int.

The reference declaration allows you to use bob and robert interchangeably.

Both refer to the same value and the same memory location.


#include <iostream>
using namespace std;
/*  www .  j ava2  s .c o m*/
int main(){
    
    int bob = 101;
    int & robert = bob;   // robert is a reference

    cout << "bob = " << bob;
    cout << ", robert = " << robert << endl;
    robert++;
    cout << "bob = " << bob;
    cout << ", robert = " << robert << endl;

    cout << "bob address = " << &bob;
    cout << ", robert address = " << &robert << endl;

    return 0; 
}

The code above generates the following result.





Note

& in the following code declares a reference type variable.

int & robert = bob;

& operator in the next statement is the address operator:

cout <<", robert address = " << &robert << endl; 

Incrementing robert by one affects both variables.

We can create both a reference and a pointer to refer to bob:

int bob = 101; 
int & robert = bob;   // robert a reference 
int * pbob = &bob;    // pbob a pointer 

Then you could use the expressions robert and *pbob interchangeably with bob and use the expressions &robert and pbob interchangeably with &bob.

We have to initialize the reference when you declare it.

A reference is rather like a const pointer, you have to initialize it when you create it.

int & robert = bob;

is, in essence, a disguised notation for something like this:

int * const pr = &bob; 

Here, the reference robert plays the same role as the expression *pr.





References as Function Parameters

References are often used as function parameters, making a variable name in a function an alias for a variable.

This method of passing arguments is called passing by reference.

The following code shows how to swap with references and with pointers.


#include <iostream>
using namespace std;
void swapr(int & a, int & b);   // a, b are aliases for ints
void swapp(int * p, int * q);   // p, q are addresses of ints
int main(){
    int my_value1 = 300;
    int my_value2 = 350;
// w w w  .  j  a  v  a2 s  . c om
    cout << "my_value1 = $" << my_value1;
    cout << " my_value2 = $" << my_value2 << endl;

    cout << "Using references to swap contents:\n";
    swapr(my_value1, my_value2);   // pass variables
    cout << "my_value1 = $" << my_value1;
    cout << " my_value2 = $" << my_value2 << endl;

    cout << "Using pointers to swap contents:\n";
    swapp(&my_value1, &my_value2); // pass addresses of variables
    cout << "my_value1 = $" << my_value1;
    cout << " my_value2 = $" << my_value2 << endl;

    return 0; 
}

void swapr(int & a, int & b)    // use references
{
    int temp;

    temp = a;       // use a, b for values of variables
    a = b;
    b = temp;
}

void swapp(int * p, int * q)    // use pointers
{
    int temp;

    temp = *p;      // use *p, *q for values of variables
    *p = *q;
    *q = temp;
}

The code above generates the following result.

Using References with a Class Object

C++ passes class objects to a function via references.

For instance, you would use reference parameters for the string, ostream, istream, ofstream, and ifstream classes as arguments.

The following code uses the string class.


#include <iostream>
#include <string>
using namespace std;
string my_func(const string & s1, const string & s2);
int main(){/*  w  ww . j  a va 2 s  . com*/
    string input;
    string copy;
    string result;

    cout << "Enter a string: ";
    getline(cin, input);
    copy = input;
    cout << "Your string as entered: " << input << endl;
    result = my_func(input, "***");
    cout << "Your string enhanced: " << result << endl;
    cout << "Your original string: " << input << endl;
 
    return 0;
}

string my_func(const string & s1, const string & s2){
    string temp;

    temp = s2 + s1 + s2;
    return temp;
}

The code above generates the following result.