Advertisement

operator[] for 2d arrays

Started by November 15, 2000 11:17 PM
7 comments, last by Quantum 24 years, 2 months ago
How do I overload operator[] so I can access 2d arrays using Something[x][y] I have a class that contains Data[MAX_X][MAX_Y], and I want to be able to access that data, without have to specify Instance.Data[x][y]. I know how to do this using operator(), and I think there''s a way using operator[]. Can anybody give me some code for this? Thanks.
I use Borland C++ which has extensions for properties which do this fairly automatically so I haven''t tried this. All I can do is offer a thought. The thought is that it is only a single operator no matter how many times you use it. Personally I would define a list and a grid. The [] operator on the grid returns a list and the [] operator on the list returns an element. That gets you grid[][] returning an element. For what you are doing you could just return a pointer to the row and let C++ handle the low order subscript. It isn''t going to know how long the row is so it can''t do the first one, but it can the second since all it needs is the type.
Keys to success: Ability, ambition and opportunity.
Advertisement
  class matrix{  protected:   float& Data[4][4];  public:   matrix();   float& operator[] (int which);}matrix::matrix(){  for (int i=0; i<4; i++)   for (int j=0; j<4; j++)     Data<i>[j] = 0.0f;}float& matrix::operator[] (int which){  // force in range...  if ((which > 3) || (which < 0)) which = 0;  return Data[which];}void main( void ){  matrix mat;  mat[3][2] = 1.0f;  float x = mat[3][2];}  


If I understand you question correctly, this is what you''re wanting, yes?


Regards,
Jumpster
Regards,JumpsterSemper Fi
Could you explain how that works. I don''t see how it would be called for the second subscript since the first one returned a float & rather than a matrix &. That first call would return data[3] rather than data[3*4] as well. [] is evaluated left to right.
Keys to success: Ability, ambition and opportunity.
Not that I''m saying you are wrong, rather that if you are right then I don''t understand why and that is the reason I don''t.
Keys to success: Ability, ambition and opportunity.
I'm not sure if Jumpster's code will work.

I'm not a C++ guru, but I don't think you can subscript references (unless they are references to pointers).

eg:

    int foo (float& stuff){    stuff[0] = 0.0f;  // illegal.    return 1;}    


If you change the &'s to *'s in Jumpster's source, it should work.

--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown

Edited by - Promiscuous Robot on November 16, 2000 3:55:54 PM
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Advertisement
You're absolutly right! My bad. Here ya go.

Here's the .H file (test.h)

    class xmatrix{  protected:   float Data[4][4];  public:   xmatrix();   float* operator[] (int which);};xmatrix::xmatrix(){  for (int i=0; i<4; i++)   for (int j=0; j<4; j++)     Data<i>[j] = 0.0f;     return;}float* xmatrix::operator[] (int which){  // force in range...  if ((which > 3) || (which < 0)) which = 0;  return Data[which];}  



And here's the main CPP file.

#include
#include "test.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
xmatrix mat;
mat[3][2] = 1.0f;
float x = mat[3][2];
return (0);
}

Sorry for the confusion.

Regards,
Jumpster



Edited by - Jumpster on November 16, 2000 4:13:05 PM
Regards,JumpsterSemper Fi
Thanks Jumpster! That''s exactly what I wanted
That one I understand.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement