Ad

Sunday, September 02, 2007

Overloading [] Operator II

In the previous article Overloading [] Operator, we overloaded the [] operator in a class to access data within the class by indexing method.

The operator [] function was defined as below:

  int myclass::operator[](int index)
  {
    // if not out of bound
    if(index<num)
      return a[index];
  }

As you can see, the above operator function is returning values, hence it could only be used on the right hand side of a statement. It’s a limitation!

You very well know that a statement like below is very common with respect to arrays:

a[1]=10;

But as I said, the way we overloaded the [] operator, statement like the one above is not possible. The good news is, it is very easy to achieve this.

For this we need to overload the [] operator like this:

  int &myclass::operator[](int index)
  {
    // if not out of bound
    if(index<num)
      return a[index];
  }

By returning a reference to the particular element, it is possible to use the index expression on the left hand side of the statement too.

The following program illustrates this:


  // Example Program illustrating
  // the overloading of [] operator
  // ----
  // now the index expression can be
  // used on the left side too
  #include <iostream.h>

  class myclass
  {
    // stores the number of element
    int num;
    // stores the elements
    int a[10];

  public:
    myclass(int num);

    int &operator[](int);
  };

  // takes the number of element
  // to be entered.(<=10)
  myclass::myclass(int n)
  {
    num=n;
    for(int i=0;i<num;i++)
    {
      cout<<"Enter value for element "<<i+1<<":";
      cin>>a[i];
    }
  }

  // returns a reference
  int &myclass::operator[](int index)
  {
    // if not out of bound
    if(index<num)
      return a[index];
  }

  void main()
  {
    myclass a(2);

    cout<<a[0]<<endl;
    cout<<a[1]<<endl;

    // indexing expression on the
    // left-hand side
    a[1]=21;
    cout<<a[1];
  }

Related Articles:

2 comments:

  1. Anonymous4:45 PM

    Good example. Hope we will se some more like this.

    Can you give some idea why we can not overload the following operators:
    1. Class member access operator
    2. :: operator
    3. sizeof operator
    4. ?: operator

    ReplyDelete
  2. Regarding your query I’d like to say the following:

    1. You can’t overload class access operators such as ‘.’ And ‘->’ as that would change the basic principle of class member access, that way one can easily overload these to access private and protected members which interferes with the basic language principle, therefore C++ does not allow us to overload such operators.

    2. Again you’re not allowed to overload ‘?:’ operator because think this way. You overload this operator and you have a line below:

    a>b?c:d;

    and

    if(a>b) a=c else a=d;

    where a,b,c,d are all objects of the same class.

    As you’d overloaded ‘?:’ operator thus both the statements would result in different things happening. Now C++ can’t let that happen, if you’ve to change the way ‘?:” evaluates expressions you can overload the ‘>’ and ‘=’ in this case, so that both the above statements results the same.

    Look, there is a basic language principle and C++ can’t let programmers change that and I most cases you don’t have to. Just like in the example above you could change the meaning of or simply overload the ‘>’ and ‘=’ operator to (basically) affect how ‘?:’ works. Same for sizeof and :: operators. You can however implement as I think, to have your own sizeof operator.

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.