Start New Discussion within our Software Development Community

Say you have a class that has members

private:
double a,b,c,d,e,f,g,h,i;

You can make an accessor like:

double operator[](int index)
  {
    if(index == 0)
      return a;
    else if(index == 1)
      return b;
    else if(index == 2)
      return c;
  }

But how would you do something like this:

double operator[][](int rindex, int cindex)
  {
    if(rindex == 0 && cindex == 0)
      return a;
    else if(rindex == 1 && cindex == 0)
      return b;
    else if(rindex == 2 && cindex == 0)
      return c;
    else if(rindex == 0 && cindex == 1)
     return d;
..... etc ...
  }

The compiler doesn't like the above attempt. The behavior I'm looking for is:

MatrixClass Matrix;
// ... fill it
double test = Matrix[0][1];

Thoughts?

Thanks,

Dave

Obviously you can't overload the [][] operator because no such operator exists. What you need to do for your MatrixClass class is overload the [] operator to return another type that also overloads the [] operator.

This article has been dead for over six months. Start a new discussion instead.