Is it possible to overload the subscript operator? If so, could someone please provide an example of doing so. I know that this must sound rather odd, but I am curious. Thank you in advance for any information you may provide! ^_^
Is it possible to overload the subscript operator? If so, could someone please provide an example of doing so. I know that this must sound rather odd, but I am curious. Thank you in advance for any information you may provide! ^_^
Yes, here's an example from one of my programs.
Code:template <class T> T& linked_list<T>::operator[](int index) { if (index < 0 || index >= list_size) throw out_of_bounds(); node<T> *temp; temp = front; for (int i = 0; i < index; ++i) { temp = temp->next; } return temp->value; }
Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup
Subscripting
gg
Only the following operators cannot be overloaded:
.
.*
::
Additionally, ?: cannot be overloaded.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Thank you all for your help! You have completly answered my question!![]()
The sizeof operator cannot be overloaded either, just to make the list completeOriginally posted by Zach L.
Only the following operators cannot be overloaded:
.
.*
::
Additionally, ?: cannot be overloaded.![]()
For completeness, neither can typeid.![]()
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.