< Previous | Contents | Next >

Passing a Constant Reference

Back in main(), I create inventory and then call display() with the following line, which passes the vector as a constant reference.

display(inventory);

198 Chapter 6 n References: Tic-Tac-Toe


This results in an efficient and safe function call. Its efficient because only a reference is passed; the vector is not copied. Its safe because the reference to the vector is a constant reference; inventory cant be changed by display().


Tra p

image

You can’t modify a parameter marked as a constant reference. If you try, you’ll generate a compile error.

image


Next, display() lists the elements in the vector using a constant reference to

inventory. Then control returns to main() and the program ends.