I have a C++ class that overloads operator[]
, the array subscript/brackets operator. This is awfully convenient outside of my class, where I can write foo[bar
]. However, I can't figure out how to use this notation when I'm implementing methods inside my class.
I know I can write operator[](bar)
or this->operator[](bar)
but those are fairly unwieldy and take away a lot of the convenience of the operator in the first place. (I also know I can just add a new method that calls the operator.) Is there a way I can write this[bar]
or this->[bar]
or something similarly nice?
Note: This question may also apply to the many unary operators (e.g., how can I call foo++
from within the class?), but I personally only care about operator[]
.
Edit: I realized soon after posting that I can use (*this)[bar]
. All of the answers so far have suggested this as well. Are there any other alternatives?