// you’re reading...

C++ Glossary

Creating a Reference Variable

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

  • C++ assigns an additional meaning to the & symbol
    • Presses it into service for declaring references
      • For example, to make rodents an alternative name for the variable rats, you could do the following (A-1):



Example of (A-1)

int rats;
int & rodents = rats; // makes rodents an alias for rats



In this context, & is not the address operator

  • Instead, it serves as part of the type identifier
    • Just as char * in a declaration means pointer-to-char, int & means reference-to-int

 


The reference declaration allows you to use rats and rodents interchangeably; both refer to the same value and the same memory location

  • (A-2) illustrates the truth of this claim

 



Example of (A-2)

// firstref.cpp -- defining and using a reference
#include <iostream>
int main()
{
	using namespace std;
	int rats = 255;
	int & rodents = rats; // rodents is a reference
 
	cout << "rats = " << rats;
	cout << ", rodents = " << rodents << endl;
	rodents++;
	cout << "rats = " << rats;
	cout << ", rodents = " << rodents << endl;
 
	// some implementations require type casting the following
	// addresses to type unsigned
 
	cout << "rats address = " << &rats;
	cout << ", rodents address = " << &rodents << endl;
	return 0;
}



Note that the & operator in the statement (A-3)

  • Is not the address operator
    • But declares that rodents is of type int &—that is, it is a reference to an int variable



Example of (A-3)

int & rodents = rats;



But the & operator in the statement (A-4)

  • Is the address operator
    • With &rodents representing the address of the variable to which rodents refers



Example of (A-4)

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



Here is the output of the program in (A-2): 

Output



As you can see both, rats and rodents have the same value and the same address

  • (The address values and display format vary from system to system.)

 


Incrementing rodents by one, affects both variables

  • More precisely, the rodents++ operation increments a single variable for which there are two names



Again, keep in mind that although this example shows you how a reference works

  • It doesn’t represent the typical use for a reference, which is as a function parameter, particularly for structure and object arguments
    • We’ll look into these uses pretty soon



References tend to be a bit confusing at first to C veterans coming to C++

  • Because they are tantalizingly reminiscent of pointers, yet somehow different
    • For example, you can create both a reference and a pointer to refer to rats (A-3)
  • Then, you could
    • Use the expressions rodents and *prats interchangeably with rats
      • And use the expressions &rodents and prats interchangeably with &rats



Example of (A-3)

int rats = 255;
int & rodents = rats; // rodents a reference
int * prats = &rats;  // prats a pointer



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 (A-4):



Example of (A-4)

int rat;
int & rodent;
rodent = rat; // No, you can’t do this



Remember

  • 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 once a reference pledges its allegiance to a particular variable, it sticks to its pledge
    • That is, (A-5)
  • (A-5) is, in essence, a disguised notation for something like this (A-6)
    • Here, the reference rodents plays the same role as the expression *pr



Example of (A-5)

int & rodents = rats;



Example of (A-6)

int * const pr = &rats;



(A-7) shows what happens

  • If you try to make a reference change allegiance from a rats variable to a bunnies variable



Example of (A-7)

// secondref.cpp -- defining and using a reference
#include <iostream>
int main()
{
	using namespace std;
	int rats = 256;
	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 = 128;
	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 (A-7):

Output Second



Initially, rodents refers to rats

  • But then the program apparently attempts to make rodents a reference to bunnies (A-8):
    • For a moment, it looks as if this attempt has succeeded because the value of rodents changes from 256 to 128
      • But closer inspection reveals that rats also has changed to 128 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 (A-9)
      • 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



Example of (A-8)

rodents = bunnies;



Example of (A-9)

rats = bunnies;



Suppose you tried the following (A-10)

  • 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



Example of (A-10)

int rats = 256;
int * pt = &rats;
int & rodents = *pt;
int bunnies = 128;
pt = &bunnies;


Discussion

No comments for “Creating a Reference Variable”

Post a comment