const string get_name()
means that the return value is constant object. It prevents the caller of the function from calling non-const methods on the temporary return value, e.g.
x.get_name() = "bla"
x.get_name()[0] = 'a';
will not compile.
I think one of the main motivations was to prevent accidental assignments e.g. in if statements:
if (x.get_name() = "John Doe"))
It has been a guideline for some time, but C++11 made it obsolete by introducing rvalue references and move semantics.
In your example, ypu should probably return a const reference to name
. This will save you the creation of temporary object and keep the benefits of being const. And the method should be declared const
because it does not modify the object's state.
const& get_name() const { return name; };