<p>For example, suppose I have a class:</p>

<pre><code>class Foo
{
public:
    std::string&amp; Name()
    {
        m_maybe_modified = true;
        return m_name;
    }

    const std::string&amp; Name() const
    {
        return m_name;
    }
protected:
    std::string m_name;
    bool m_maybe_modified;
};
</code></pre>

<p>And somewhere else in the code, I have something like this:</p>

<pre><code>Foo *a;
// Do stuff...
std::string name = a-&gt;Name(); // &lt;-- chooses the non-const version
</code></pre>

<p>Does anyone know why the compiler would choose the non-const version in this case?</p>

<p>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.        </p>

Two answers spring to mind:
The non-const version is a closer match.
If it called the const overload for the non-const case, then under what circumstances would it
ever
call the non-const overload?
You can get it to use the other overload by casting
```
a
```
to a
```
const Foo *
```
.
Edit:
From
C++ Annotations
Earlier, in section 2.5.11 the concept
of function overloading was
introduced. There it noted that member
functions may be overloaded merely by
their const attribute. In those cases,
the compiler will use the member
function
matching most closely the
const-qualification of the object: