Overloading The Subscript Operator:

 < Day Day Up > 



Overloading The Subscript Operator: [ ]

In cases where your class contains an array, you can make interaction with the array more natural by overloading the array subscript operator. The subscript operator must be declared as a class member function.

Example 14.16 gives the declaration for a class named DynamicArray.

Listing 14.16: dynamicarray.h

start example
  1  #ifndef _DYNAMIC_ARRAY_H  2  #define _DYNAMIC_ARRAY_H  3  4  class DynamicArray{  5    public:  6       DynamicArray(int _size = 5);  7       virtual ~DynamicArray();  8       int& operator[](unsigned i);  9       int getSize(); 10    private: 11       int* its_array; 12       int size; 13  }; 14  #endif
end example

The DynamicArray class implements a dynamically allocated array of integers. It will automatically expand as necessary to accommodate insertions past its current size.

The declaration of the overloaded subscript operator appears on line 8. This version of the operator declares one parameter, an unsigned integer named i. The parameter will actually be the value that appears between the brackets when the implicit call to the subscript operator is made. In the case of DynamicArray objects, this call will look like what you have seen with ordinary arrays. For example, given a DynamicArray object named d1, the integer value 23 can be inserted into d1's array position 0 with the following code:

d1[0] = 23;

Example 14.17 shows the dynamicarray.cpp file.

Listing 14.17: dynamicarray.cpp

start example
  1  #include "dynamicarray.h"  2  3  DynamicArray::DynamicArray(int _size):size(_size){  4       its_array = new int[_size];  5       for(int i=0; i<size; i++)  6           its_array[i] = 0;  7  }  8  9  DynamicArray::~DynamicArray(){ 10       delete[] its_array; 11  } 12 13  int& DynamicArray::operator[](unsigned i){ 14       if(i >= (size)){ 15          int newsize = size+10; 16          int* temp = new int[size]; 17          for(int j = 0; j<size; j++){ 18               temp[j] = its_array[j]; 19           } 20           delete[] its_array;   21           its_array = new int[newsize]; 22           for(int j = 0; j<size; j++){ 23               its_array[j] = temp[j]; 24           }  25 26           for(int j=size; j<newsize; j++){ 27           its_array[j] = 0; 28           } 29          delete[] temp; 30          size = newsize; 31 32         return its_array[i]; 33       } else return its_array[i]; 34  } 35 36  int DynamicArray::getSize(){ return size;}
end example

Note the implementation of the subscript operator and the use of the parameter i. The parameter is not the value being inserted into the array. The parameter is used as the index into the array. If you take away all the dynamic sizing code all the subscript operator really does is return a reference to the value located at the array position indicated by the parameter value. If the value of i is greater than or equal to the current size of the array the array is increased in size and then value is returned, other wise, the value is simply returned.

In this example the parameter is an integer value. However, the parameter could be anything. For instance, the parameter could be a string of characters used to locate entries in an associative array. Example 14.18 gives a main() function showing a DynamicArray object in action.

Listing 14.18: main.cpp

start example
  1  #include <iostream>  2  using namespace std;  3  #include "dynamicarray.h"  4  5  int main(){  6      DynamicArray d1;  7  8      for(int i=0; i<6; i++){  9          d1[i] = i; 10      } 12 13      int j = d1[3]; 14      cout<<"j: "<<j<<endl; 15 16      for(int i=0; i<d1.getSize(); i++){ 17          cout<<d1[i]<<endl; 18      } 19 20      return 0; 21  }
end example

Figure 14-8 shows the results of running example 14.18.


Figure 14-8: Results of Running Example 14.18

On line 6 of example 14.18 an instance of DynamicArray is instantiated with its default array size of 5. The for statement on line 8 inserts 6 integer values into the array. When the 6th integer is inserted the array resizes itself allowing the insertion.

Notice how the overloaded subscript operator makes handing DynamicArray objects easier. The code on line 13 shows how the subscript operator can be used on the right hand side of the assignment operator as well as on the left hand side.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net