Your mutable version is fine:
T& operator[](T u);
but the const
version should be a const
member function as well as returning a const
reference:
const T& operator[](T u) const;
^^^^^
This not only distinguishes it from the other overload, but also allows (read-only) access to const
instances of your class. In general, overloaded member functions can be distinguished by their parameter types and const/volatile qualifications, but not by their return types.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…