Then you could use the expressions rodents and *prats interchangeably with rats and use the expressions &rodents and prats interchangeably with &rats. From this standpoint, a reference looks a lot like a pointer in disguised notation in which the * dereferencing operator is understood implicitly. And, in fact, that’s more or less what a reference is. But there are differences besides those of notation. For one, it is necessary to initialize the reference when you declare it; you can’t declare the reference and then assign it a value later the way you can with a pointer:

int rat;

int & rodent;

rodent = rat;   // No, you can't do this.

Note

You should initialize a reference variable when you declare it.

A reference is rather like a const pointer; you have to initialize it when you create it, and when a reference pledges its allegiance to a particular variable, it sticks to its pledge. That is,

int & rodents = rats;

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

int * const pr = &rats

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

Listing 8.3 shows what happens if you try to make a reference change allegiance from a rats variable to a bunnies variable.

Listing 8.3. secref.cpp

// secref.cpp -- defining and using a reference

#include

int main()

{

    using namespace std;

    int rats = 101;

    int & rodents = rats;   // rodents is a reference

    cout << "rats = " << rats;

    cout << ", rodents = " << rodents << endl;

    cout << "rats address = " << &rats

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

    int bunnies = 50;

    rodents = bunnies;       // can we change the reference?

    cout << "bunnies = " << bunnies;

    cout << ", rats = " << rats;

    cout << ", rodents = " << rodents << endl;

    cout << "bunnies address = " << &bunnies

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

    return 0;

}

Here’s the output of the program in Listing 8.3:

rats = 101, rodents = 101

rats address = 0x0065fd44, rodents address = 0x0065fd44

bunnies = 50, rats = 50, rodents = 50

bunnies address = 0x0065fd48, rodents address = 0x0065fd4

Initially, rodents refers to rats, but then the program apparently attempts to make rodents a reference to bunnies:

rodents = bunnies;

For a moment, it looks as if this attempt has succeeded because the value of rodents changes from 101 to 50. But closer inspection reveals that rats also has changed to 50 and that rats and rodents still share the same address, which differs from the bunnies address. Because rodents is an alias for rats, the assignment statement really means the same as the following:

rats = bunnies;

That is, it means “Assign the value of the bunnies variable to the rat variable.” In short, you can set a reference by an initializing declaration, not by assignment.

Suppose you tried the following:

int rats = 101;

int * pt = &rats

int & rodents = *pt;

int bunnies = 50;

pt = &bunnies

Initializing rodents to *pt makes rodents refer to rats. Subsequently altering pt to point to bunnies does not alter the fact that rodents refers to rats.

References as Function Parameters

Most often, references are used as function parameters, making a variable name in a function an alias for a variable in the calling program. This method of passing arguments is called passing by reference. Passing by reference allows a called function to access variables in the calling function. C++’s addition of the feature is a break from C, which only passes by value. Passing by value, recall, results in the called function working with copies of values from the calling program (see Figure 8.2). Of course, C lets you get around the passing by value limitation by using pointers.

Figure 8.2. Passing by value and passing by reference.

Let’s compare using references and using pointers in a common computer problem: swapping the values of two variables. A swapping function has to be able to alter values of variables in the calling program. That means the usual approach of passing variables by value won’t work because the function will end up swapping the contents of copies of the original variables instead of the variables themselves. If you pass references, however, the function can work with the original data. Alternatively, you can pass pointers in order to access the original data. Listing 8.4 shows all three methods, including the one that doesn’t work, so that you can compare them.

Listing 8.4. swaps.cpp

// swaps.cpp -- swapping with references and with pointers

#include

void swapr(int & a, int & b);   // a, b are aliases for ints

void swapp(int * p, int * q);   // p, q are addresses of ints

void swapv(int a, int b);       // a, b are new variables

int main()

{

    using namespace std;

    int wallet1 = 300;

    int wallet2 = 350;

    cout << "wallet1 = $" << wallet1;

    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using references to swap contents:\n";

    swapr(wallet1, wallet2);   // pass variables

    cout << "wallet1 = $" << wallet1;

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги