[cs23021] Questions

Mikhail Nesterenko mikhail at cs.kent.edu
Fri Nov 19 14:32:16 EST 2010


> 
> How is the overloaded operator done for something like "arr[5]" with the the
> "[", argument (5) and "]" ? Is there a way to extract the argument ?

The subscript operator can be overloaded as any other. Here is the
prototype of overloaded (member function) subscript operator:

    int&  operator[] (const int Index);


Then, when the object of this class can be used as an array. This
comes handy when this object collects something similar to array of
values. For example, it is a vector.

Note that the overloaded assignment returns a reference. We have not
studied returning references in this class. This is needed for proper
handling of overloaded subscript operator on the left hand side of the
assignment.

Here is a good article describing overloaded subscript.

http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/

Thanks,
--
Mikhail


More information about the cs23021-2 mailing list