Skip to main content

Section 10.3 Constant References

Pass by reference allows a function to modify the data it is passed. This can be useful, especially if we are working with a large piece of data. Imagine we have a function to make a string lower case. If that string uses pass by value, it needs to make a copy of my string, modify that copy, then return the copy so the caller can use the new string. For a very long string, that making those extra copies could be expensive. If the function uses pass by reference, it can just modify the string that was passed in without having to make copies.
Sometimes we want to pass a reference to a function to avoid extra copies but we don’t want that function to be able to modify the data. Say we have a function countEs that will count the number of Es that appear in the string. We don’t want the function to change the string, which countEs(string& s) would allow. But we also don’t want to copy the parameter, which `countEs(string s) would do.
The solution is to use a const reference. A const reference is a reference that promises not to change the data that is being pointed to: `add(const int& x)`. Remember that it helps to read declarations backwards “x is a reference to an int that shall remain constant”. The compiler will treat that declaration as a promise. If you write any code that tries to modify what the const reference points at, it will be a compile error:
Listing 10.3.1.

Insight 10.3.1.

Other than one detail described below, declaring a reference to be const doesn’t allow it to do anything new. It just asks the compiler to help avoid accidental changes to the referenced value. It is a way for a function to make a binding promise to the caller: “you are going to give me access to some of your memory, but I promise I won’t change that memory.”
The one extra things you can do with a const reference: a const reference is allowed to reference a const variable (a normal constant). A normal reference is not allowed to. Otherwise we could use the reference to change the const. We won’t need to use this feature.
You have attempted of activities on this page.