Skip to content

Files

Latest commit

 

History

History

15

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Exercise 6.15

Explain the rationale for the type of each of find_char's parameters. In particular, why is s a reference to const but occurs is a plain reference? Why are these parameters references, but the char parameter c is not? What would happen if we made s a plain reference? What if we make occurs a reference to const?

Solution

s is a reference to const because the function does not edit s, while the function does edit occurs. c could be a reference to const, such that passing a char literal would still work, but c cannot be a plain reference, otherwise find_char(s, 'o', ctr) would not work. The other parameters are references to avoid unneccesary copying of the arguments. If s was a plain reference, then the function would have to copy the entire string every time called. If occurs was a reference to const, then its value could not be edited by the function and ++occurs would not work.