For example, suppose I have a class:
class Foo
{
public:
std::string& Name()
{
m_maybe_modified = true;
return m_name;
}
const std::string& Name() const
{
return m_name;
}
protected:
std::string m_name;
bool m_maybe_modified;
};
And somewhere else in the code, I have something like this:
Foo *a;
// Do stuff...
std::string name = a->Name(); // <-- chooses the non-const version
Does anyone know why the compiler would choose the non-const version in this case?
This is a somewhat contrived example, but the actual problem we are trying to solve is periodically auto-saving an object if it has changed, and the pointer must be non-const because it might be changed at some point.