Suppose I have a class Foo with a std::string member str. What should get_str return?
std::string Foo::ge
It depends on what you want to do with the return value.
This is better if you just want to make a query and not modify str.
const std::string& Foo::get_str() const
{
return str;
}
Otherwise, go for this:
std::string& Foo::get_str()
{
return str;
}
And if you want a copy/clone of str, then use this:
std::string Foo::get_str() const
{
return str;
}