In C++, we can define and overload operators to make the code beautiful. However, when an operator needs to be called from within the class which defines it, the syntax isn't as elegant.
For example, consider the typical use of operator[]
on a string
:
std::string word("Hello!");
char first = word[0];
Simple. But what if such an operator needs to invoked from within some class which itself defines it? The syntaxes aren't as pretty:
(*this)[i];//option 1
operator[](i);//option 2
this->operator[](i);//option 3
In terms of performance and/or behavior, what is the difference between the three, especially when dealing with inheritance or polymorphism? Which is the proper, unambiguous syntax?