C++ contains a feature that is related to the pointer called a reference. A reference is basically an implicit pointer. There are two ways that a reference can be used: as a function parameter and Independent References.
1. Reference Parameters: The most important use for a reference is to allow you to create functions that automatically use call-by-reference parameter passing. Arguments can be passed to functions in one of two ways: using call-by-value or call-by-reference. When using call-by-value, a copy of the argument is passed to the function. Call-by-reference passes the address of the argument to the function. By default, C++ uses call-by-value, but it provides two ways to achieve call-by-reference parameter passing. First, you can explicitly pass a pointer to the argument. Second, you can use a reference parameter. For most circumstances the best way is to use a reference parameter. To fully understand what a reference parameter is and why it is valuable, we will begin by reviewing how a call-by-reference can be generated using a pointer
Parameter. The following program manually creates a call-by-reference parameter using a pointer in the function called neg( ), which reverses the sign of the integer variable pointed to by its argument.
// Manually create a call-by-reference using a pointer.
#include
using namespace std;
void neg(int *i);
int main()
{
int x;
x = 10;
cout << x << " negated is ";
neg(&x);
cout << x << "\n";
return 0;
}
void neg(int *i)
{
*i = -*i;
}
In this program, neg( ) takes as a parameter a pointer to the integer whose sign it will reverse. Therefore, neg( ) must be explicitly called with the address of x. Further, inside neg( ) the * operator must be used to access the variable pointed to by i. This is how you generate a "manual" call-by-reference in C++, and it is the only way to obtain a call-by-reference using the C subset. Fortunately, in C++ you can automate this feature by using a reference parameter.
2. Independent References:
By far the most common uses for references are to pass an argument using call-by-reference and to act as a return value from a function. However, you can declare a reference that is simply a variable. This type of reference is called an independent reference. When you create an independent reference, all you are creating is another name for an object variable. All independent references must be initialized when they are created. The reason for this is easy to understand. Aside from initialization, you cannot change what object reference variable points to. Therefore, it must be initialized when it is declared.
This program illustrates an independent reference:
#include
using namespace std;
int main( )
{
int a;
int &ref = a; // independent reference
a = 10;
cout << a << " " << ref << "\n";
ref = 100;
cout << a << " " << ref << "\n";
int b = 19;
ref = b; // this puts b's value into a
cout << a << " " << ref << "\n";
ref--; // this decrements a
// it does not affect what ref refers to
cout << a << " " << ref << "\n";
return 0;
}
The program displays this output:
10 10
100 100
19 19
18 18
Actually, independent references are of little real value because each one is, literally, just another name for another variable. Having two names to describe the same object is likely to confuse, not organize, your program.