Just a C++ newbie question.
I have a class like
class Person
{
private:
std::string m_name;
};
Is it better to design getter as
std::string name() const;
so m_name will be copied each time name() is called or as
const std::string& name() const;
so caller can make his own copy, if he wants.
In general, I would go with the const& version, since it avoids unnecessary copying.
However, if you’re making a class that’s intended to be accessed by multiple threads simultaneously, returning a copy is cleaner and more reliable.