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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.