John Goche wrote:
I have come across the following class functions:
T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }
However, I have noticed that they have the same signature.
I thought it was not possible to define two class functions
with the same signature. How can the compiler differentiate
between these two?
Both are member functions of a class X. So when you have an object x of type
X, and you do x[...], then the non-const version will be called. But if x
is of type const X, then x[...] resolves to the const version. Its the
constness or non-constness of the object x that determines which member
function is chosen for x[...].
Best
Kai-Uwe Bux