T& f() { // some code ... }
const T& f() const { // some code ... }
I've seen this a couple of times now (in the introductory book I've been studying thus far). I know that the first const makes the return value const, in other words: unmodifiable. The second const allows that the function can be called for const declared variables as well, I believe.
But why would you have both functions in one and the same class definition? And how does the compiler distinguish between these? I believe that the second f() (with const) can be called for non-const variables as well.
See Question&Answers more detail:os