Overloading Indexing Operator ([ ])

Written by Raza on. Posted in C++

Up till now we have overloaded assignment operator and then the addition operator. One another operator which we quite frequently use in our program and thus need to redefine for ourselves is the indexing operator ([ ]). Indexing operator in in fact used to dereference a given index of the array. After overloading indexing operator, we can directly dereference the object for our particular index. Let us see how we can efficiently do this.

Normally we dereference a pointer thus.

</p>
<p>int i = 0 ;</p>
<p>int *ptr ;</p>
<p>cout &lt;&lt; (*ptr + i) ;</p>
<p>

 

It is equal to this syntax.

</p>
<p>ptr[i];</p>
<p>

 

Now we are working once again with our Vector class. If we want to use the value of any index of the array within, we cannot do so without making getter and setter functions because the array is a private data member. Let us see how we declare and use these setters and getters. Remember that setter is a function used to change the value of private data member of a class from outside of the class. Getter is a function only to retrieve the value at a given index.

 

</p>
<p>int getindex (const int i)<br />
{<br />
return this-&gt;arr [i] ;<br />
}</p>
<p>&nbsp;</p>
<p>void setindex (const int i, const int val)</p>
<p>{<br />
this-&gt;arr [i] = val ;<br />
return this-&gt;arr [i] ;<br />
}</p>
<p>&nbsp;</p>
<p>int main (int argc, char*argv[])</p>
<p>{</p>
<p>Vector v1 ;</p>
<p>v1.setindex (5, 10) ;</p>
<p>cout &lt;&lt; v1.getindex (5) ;</p>
<p>return 0 ;<br />
}</p>
<p>

 

This code is good enough, but making two functions and calling them separately time and again is cumbersome in a large program. We the human beings like to work the simpler way. Instead of a setter and a getter function it would be a hell lot better if we simple use this syntax to input our output the value at a particular index.

 

</p>
<p>v1 [i] = 5;</p>
<p>cout &lt;&lt; v1 [i] ;</p>
<p>

This requires overloading the indexing operator.

The body of overloaded function for indexing operator would be very similar to the above written setter function. The difference would be to return the index value be reference instead of by value. That would enable us not only to read but also to write the value at our index. Also make sure to check that the index provided is not out of bounds of the array size. Have a look at this syntax.

 

</p>
<p>v1 [1] = v1 [3];</p>
<p>

On the left hand side of the (=) operator, we need the overloaded function to return by reference because we are changing its value. On its right hand side, returning by value is sufficient. Now we write the overloaded function for indexing operator.

</p>
<p>int&amp; Vector :: operator [ ] (const int i)<br />
{</p>
<p>&nbsp;</p>
<p>if (i &lt;= this-&gt;size)<br />
return this-&gt;arr[i] ;</p>
<p>else<br />
return this-&gt;arr[0] ;</p>
<p>&nbsp;</p>
<p>}</p>
<p>

Note that in case of array index exceeding its bounds, the best we can do for now is to return the value of the first operator only or just output an error message. But when we learn exception handling, we would be able to better handle this situation. Until that time, Good Luck with this strategy!

Tags: , ,