The std::string
accessors (back
, front
, at
, and operator[]
) have const
and non-const
overloads, as below:
char& x();
const char& x() const;
Why does the second version return a const
reference, as opposed to simply returning the char
by value (as a copy)?
According to the rules of thumb on how to pass objects around, shouldn't small objects be passed by value when there's no need to modify the original?
Because the caller might want a reference to the char, that reflects any changes made to it through non-const avenues.