You cannot return a permalink to an element in a constant function. The usual two overloads for direct access to access elements are:
const Foo & foo() const { return m_foo; } Foo & foo() { return m_foo; }
Inside a constant function, this is equal to const T * (where T is your class). Thinking of your class as a dumb C structure for a minute, you return *this->m_foo , but this constant, when this is a pointer to a constant, so you cannot refer to this constant.
The return by value is accurate because you call the copy constructor Foo , which has the signature Foo(const Foo&) - so it can copy from permalinks:
Foo copy_foo() const { return m_foo; }
source share