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 break down 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/function.'Of course, C lets you get arround the passing by value limitation by using pointers.
Using passing by value, if a function has one argument, makes two variables, one the argument and one variable in the function initialized with the value of the argument.
Using passing by reference, if a function has one argument, makes one variable, the argument, and a reference in the function to that variable, which is two names for one variable.
Functions that pass by value, can use many kinds of actual arguments:
double cube(double a)
{
a *= a * a;
return a;
}
double z = cube(x + 2.0); // evaluate x + 2.0, pass value
z = cube(8.0); // pass the value 8.0
int k = 10;
z = cube(k); // convert value of k to double, pass value
double yo[3] = { 2.2, 3.3, 4.4};
z = cube(yo[2]); // pass the value 4.4
Suppose you try similar arguments for a function with a reference parameter:
double refcube(double & ra)
{
ra *= ra * ra;
return ra;
}
It would seem that passing a reference should be more restrictive. After all, if ra is the alternative name for a variable, then the actual arguments should be that variable. Something like:
double z = refcube(x + 3.0); // may not compile
doesn't appear to make sense because the expression x + 3.0 is not a variable. For example you can't assign a value to such expression:
x + 3.0 = 5.0 // nonsensical
In some cases, the compiler will accept x + 3.0.
What happens is that because x + 3.0 is not a type double variable, the program creates a temporary, nameless variable, intializing it to the value of the expression x + 3.0. Then ra becomes a reference to that temporary variable. Let's take a closer look at temporary variables and see when they are and are not created.
C++ can generate a temporary variable if the actual argument doesn't match a reference argument. Currently C++ permits this only if the argument is a const reference, but this is a new restriction.
Provided that the reference parameter is a const, the compiler generates a temporary variable in two kinds of situations:
- When the actual argument is the correct type but isn't a lvalue
- When the actual argument is of the wrong type, but it's of a type that can be converted to the correct type
An argument that is an lvalue is a data object that can be referenced. For example, a variable, an array element, a structure member, a reference, and a dereferenced pointer are lvalues.
Non-lvalues include literal constants and expressions with multiple terms. For example, supose you redifine refcube() so that it has a constant reference argument:
double refcube(const double &ra)
{
return ra * ra * ra;
}
Now consider the following code:
double side = 3.0;
double * pd = &side;
double & rd = side;
long edge = 5L;
double lens[4] = { 2.0, 5.0, 10.0, 12.0};
double c1 = refcube(side); // ra is side
double c2 = refcube(lens[2]); // ra is lens[2]
double c3 = refcube(rd); // ra is rd is side
double c4 = refcube(*pd); // ra is *pd is side
double c5 = refcube(edge); // ra is temporary variable
double c6 = refcube(7.0); // ra is temporary variable
double c7 = refcube(side + 10.0); // ra is temporary variable
The arguments side, lens[2], rd and *pd are type double data objects with names, so it is possible to generate a reference for them, and no temporary variables are needed. (Recall that an element of an array behaves like a variable of the same type as the element.)
But although edge is a variable, it is of the wrong type. A reference to a double can't refer to a long. The arguments 7.0 and side + 10.0, on the otherhand, are the right type, but they are not named data objects. In each of these cases, the compiler generates a temporary, anonymous variable and makes ra refer to it. These temporary variables last for the duration of the function call, but then the compiler is free to dump them.
So why is this behaviour okay for constant references but not otherwise?
void swapr(int & a, int & b) // use references
{
int temp;
temp = a; // use a, b for values of variables
a = b;
b = temp;
}
What would happen if you did the following under the freer rules of early C++?
long a = 3, b = 5;
swapr(a, b);
Here there is a type mismatch, so the compiler would create two temporary int variables, initialize them to 3 and 5, and then swap the contents of the temporary variables, leaving a and b unaltered,
The solution is to prohibit creating temporary variables in these situations, and that is what the C++ standard now does!
Now think about the refcube() function. Its intent is merely to use passed values, not to modify them, so temporary variables cause no harm and make the function more general in the sort of arguments is can handle. Therefor, if the declaration states that a reference is const, C++ generates temporary variables when necessary. In essence, a C++ function with a const reference formal argument and a nonmatching actual argument mimics the traditional passing by value behavior, guaranteeing that the original data is unaltered and using a temporary variable to hold the value.
REMEMBER:
If a function call argument isn't an lvalue or does not match the type of the corresponding const reference parameter, C++ creates an anonymous variable of the correct type, assigns the value of the function call argument to the anonymous variable, and has the parameter refer to that variable.
Abonneren op:
Reacties posten (Atom)
Geen opmerkingen:
Een reactie posten