Lab 8: Experiment 6

Constant Reference Parameters

We've used constants to store constant information just by slapping const in front of the declaration. But can we do this in front of a parameter?

As we've seen, value parameters don't change their arguments, although reference parameters do. While reference parameters offer up opportunities for passing information out of a function, that could be dangerous if we didn't want to change the parameter.

Issue: Can we declare a reference parameter to be constant, and can we change its value?

Hypothesis: If we can declare a constant reference parameter, we cannot change its value.

Considering what "constant" means, this sound very reasonable.

Experiment: We can test our hypothesis by making one of the parameters in change() a constant reference parameter. Such a change must be made in both the prototype:

void change(int & param1, int param2, const int & param3);
and in the definition:
void change(int & param1, int param2, const int & param3)
  param1 = 1;
  param2 = 2;
  param3 = 3;
This is a simple change to make after the previous experiment.

Observation: Compile and execute your program.

Question #8.6.1: Can you compile the program? If not, what error message do you get? If you can, what is displayed by the program?


Question #8.6.2: Is our hypothesis correct? How do you know? If it's not correct, what should it be?

Be careful with the form of our hypothesis. There are actually two issues you need to decide. First, can you declare a constant reference parameter? Second, can you change its value? Now if the question to first is false, then the second is moot. Observe carefully where the compiler complains about your code.

So why bother with a constant reference? We want a reference for efficiency. If we have a string parameter, that's could be a lot of information. A person's name can be 20 to 100 characters long. By making a copy of that data, the program has to copy all of those characters for the function. But a reference is about the same size as an int (depending on the machine, operating system, and perhaps the compiler). It doesn't matter how many characters are in the string, a reference to a string is always the same small size.

However, we run the danger of changing the string in our function, and that's why we want a constant reference. To protect ourselves we can tell the compiler that we really want to keep the string constant. "Don't let me change it!"

As you might imagine, this applies to any complicated object, not just strings. A really good rule of thumb is to pass in primitive types (e.g., int, double, bool, char, etc.) by value unless you have to change their value; it might actually be less efficient to pass them by reference! But all other type objects, especially if you have to load a library to use them, should be passed by reference or constant reference.

Back to the Lab Exercise
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.