samedi 13 juin 2015

Why does the const overload of std::string::back/front return a reference?

The prototypes of std::string::back are:

char& back();
const char& back() const;

Same applies to std::string::front.

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?

Aucun commentaire:

Enregistrer un commentaire