Possible Duplicate:
Should accessors return values or constant references?
First of all, let’s ignore the setters and getters are/aren’t evil. 🙂
My question is, if I have a class that has some std:: container as a member, let’s say string, what should the return type of the getter be? I kind of prefer const T& compared to T for performance reasons… I know that most of the time users will make a copy anyway, but I guess not all the time. Am I wrong?
So in general what is better:
std::string get_name() const;
OR
const std::string& get_name() const;
Return a constant reference. If the user wants to make a copy, no skin off your back.