Syntax
void printName(const std::string& name)
{
std::cout << name;
}
std::string name { "Ada" };
printName(name);
The reference refers to the caller’s object. const prevents the function from modifying that object through the parameter.
When to use it
- The object is required and must not be null
- Copying the object may be expensive
- The function only needs to read the object