Reference
Consider the following code:
char & ref = var;
ref = 'Z';
cout << var << ' ' << ref << endl;
The output is: Z Z
The first statement in the code begins with the declaration and assignment to the char variable, var, the value, ‘Y’. The second statement has a second variable called ref. It is still of type, char. However, here, there is the & symbol between char and the variable, ref. It does not matter if & is closer to char or to ref.
To ref is assigned the variable, var. What does this mean? Because of & between char and ref, ref and var become alternative names for the same memory location that has the character, ‘Y’. Now, either ref or var can be used to change the value in the memory location. When that happens, both var and ref would return the same value. In the above code, ref was used to change the value.
In the following code, the content of the same memory location, referred to by var and ref, is changed through var:
char & ref = var;
var = 'Z';
cout << var << ' ' << ref << endl;
The output is the same: Z Z
A reference variable is a synonym for some original variable. The original variable itself, is still a reference.
There can be more than one reference to an original variable, as the following code shows:
char & ref1 = var;
char & ref2 = var;
ref2 = 'Z';
cout << var << ' ' << ref1 << ' ' << ref2 << endl;
The output is: Z Z Z
To have a reference, assign an original variable to a new variable; in the declaration of the new variable, have & between the type and the new variable.
The advantage of using reference variable, is that, there is only one copy of the value in memory, for the different variable names (synonyms). With normal programming in C++, if the different memory locations are not needed, and each variable has its own memory location, with the same value, this would be waste of memory.
Vector and Reference
Vector is a class from which vector objects are created (instantiated). In order to use the vector class, the vector library has to be included into the program. There is the original vector object variable. To this, other reference (synonym) variables can be associated. Making a vector reference variable is done at declaration as with the above fundamental object (char). The following program illustrates this:
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> vtr;
vector<string> &vtrRef1 = vtr;
vector<string> &vtrRef2 = vtr;
return 0;
}
The vector library is included into the program. A vector of strings is intended, so the string library is also included. Note the use and position of & in the second and third vector declarations. All three vector object variables, vtr, vtrRef1 and vtrRef2 are synonyms or references to the same memory location.
vtrRef1 and vtrRef2 are used in a program in the same way as vtr, without preceding any with &. In other words, vtr[5], vtrRef1[5] and vtrRef2[5] world return the same value located in the same memory location.
Advantage of Using a Reference Vector
The content of a vector object can be very long. It is usually not desirable to have multiple copies of the same long list in memory. From time to time, it is desirable to have two references of the same vector in memory. This is particularly useful when passing a vector to a function. If the vector is not passed by reference (or pointer), there will be two copies of the same vector in memory in the program. That is, the function body will have a copy of the vector in memory that is different from the original vector outside the function body. One way to avoid such two copies but still have two variables, is to pass by reference. In this way, the variable in the function body, and the variable outside the function body would both refer to the same vector.
Passing a Vector by Reference to a Function
It is simple to pass a vector by reference to a function. To do this, have the original vector outside the function; let the parameter of the function definition be the declaration of the vector with the ampers-and (&), between the vector type and the parameter name. The parameter name of the vector and the original name of the vector can be different. The original name of the vector is the argument of the function call. In this way, the vector name in the function body, and the vector name outside the function body, are two different names referring to the same vector.
Imagine a vector of animal names. The heading for the program would be:
#include <vector>
#include <string>
using namespace std;
The vector outside the function can be:
The function of interest can be:
for (int i=0; i<vtr.size(); i++)
cout << vtr[i] << ", ";
cout << endl;
}
At this point, the name of the vector outside the function is animals, and the name of the vector inside the function is vtr. Both names refer to the same vector object in memory. If any element of the vector is changed inside the function, the change will be seen in the vector variable outside the function. If any element of the vector is changed outside the function, the change will be seen in the vector variable inside the function. No such change has been made in this program, though. The function simply just displays the vector content, which is the same inside the function and outside the function. Note the use and position of the symbol, & in the parameter declaration. The C++ main function can be:
{
fn(animals);
return 0;
}
The variable name of the vector outside the function is the argument of the function call. The output is:
Note: as soon as the function starts executing, the following statement is made in the function:
This declaration is very similar to a fundamental object declaration above, which is:
Conclusion
A normal name and the referenced name of a vector are both synonyms of the same vector. This means that they refer to the same memory location. To pass a vector to a function by reference, the parameter declaration of the vector in the parentheses of the function needs to have & between the vector type and the vector name. This name in the parentheses is officially the referenced name. At the function call, the normal name of the original vector variable outside the function definition becomes the argument of the function. The two names can be different. Within the function definition, & does not preceded the vector name.