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! ^_^
Printable View
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;
}
Subscripting
gg
Only the following operators cannot be overloaded:
.
.*
::
Additionally, ?: cannot be overloaded.
Thank you all for your help! You have completly answered my question! :)
The sizeof operator cannot be overloaded either, just to make the list complete :)Quote:
Originally posted by Zach L.
Only the following operators cannot be overloaded:
.
.*
::
Additionally, ?: cannot be overloaded.
For completeness, neither can typeid. ;)