6
#define ROW 3
#define COL 4

class Matrix
{
   private:
      int mat[ROW][COL];  
    //.....
    //.....

};

int main()
{
  Matrix m;
  int a = m[0][1]; //  reading
  m[0][2] = m[1][1]; // writing
} 

I think directly it not possible to overload [][] .

I think i have to do it indirectly but how to implement it?

Ashish
  • 8,441
  • 12
  • 55
  • 92

2 Answers2

11

The easier solution is to use the operator() as it allows multiple parameters.

class M
{
    public:
       int& operator()(int x,int y)  {return at(x,y);}
    // .. Stuff to hold data and implement at()
};


M   a;
a(1,2) = 4;

The easy way is that the first operator[] returns an intermediate object that the second operator[] returns the value from the array.

class M
{
    public:
    class R
    {
         private:
             friend class M; // Only M can create these objects.
             R(M& parent,int row): m_parent(parent),m_row(row) {}
         public:
              int& operator[](int col) {return m_parent.at(m_row,col);}
         private:
              M&  m_parent;
              int m_row;
    };

    R operator[](int row) {return R(*this,row);}

    // .. Stuff to hold data and implement at()
};

M   b;
b[1][2] = 3;   // This is shorthand for:

R    row = b[1];
int& val = row[2];
val      = 3;
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Would a good optimizer change the proxy version into just as efficient code as `operator()`? – GManNickG Dec 28 '09 at 19:56
  • That depends on the compiler. But I like to make the use of my objects as intutaive as possable. The more natural it feels to use the object the less likely the opertunity for mistakes. I personally prefer the proxy but its six of one half a dozen of the other for this situation and I could not say definateively that one is better than the other. – Martin York Dec 28 '09 at 20:05
  • Yea, I've always used `operator()` but I think I'll use a proxy this time around (I write game engines) and see if it creates any problems. I can't imagine it would be too hard for the compiler to make it just as fast, since it'll have the full definition of all the operators. – GManNickG Dec 28 '09 at 20:07
  • should be: int& operator () (int x,int y) – dvide Dec 28 '09 at 20:07
  • So impressed! I just got this to work with a templated class (for Matrix operations). I had one minor syntax error cuz I was trying to return a R& from R::operator[] instead of R itself. Once I caught that it worked no problem. Very slick! (now to see if I can overload the "address of" operator so I can return a vector within the matrix. :P – Troy Chard Mar 04 '11 at 03:28
1

Since you want to store your elements in fixed-size arrays it'll be fairly easy:

#define ROWS 3
#define COLS 4

typedef int row_type[COLS];

class matrix {
   row_type elements[ROWS];
public:
   ...
   row_type const& operator[](int r) const {return elements[r];}
   row_type      & operator[](int r)       {return elements[r];}
   ...
};

That should work.

In addition, you may want to replace the #defines with proper constants or use template parameters for type (int) and size (3x4) to make your matrix class more generic. If you want to support dynamic sizes your operator[] needs to return proxy-objects. It's possible but you should probably prefer operator() with two index parameters for element access.

sellibitze
  • 27,611
  • 3
  • 75
  • 95