Archived

This topic is now archived and is closed to further replies.

matrix operator overloading

This topic is 6368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I had a simple class such as this: class matrix { public: matrix(int rows, int cols); //allocate a rows x cols matrix //etc, etc.... private: int **theMatrix; //will be allocated dynamically int rows; int cols; }; How do you overload the [] operator to properly do this: matrix someMatrix(5,5); //make a 5 x 5 matrix //do whatever...... cout << someMatrix[2][4] << endl; I know how to do it with a single dimension array, but how do you do this with a two dimension array? thanks, skitzo_smurf Edited by - skitzo_smurf on 7/11/00 4:37:20 PM Edited by - skitzo_smurf on 7/11/00 5:45:21 PM

Share this post


Link to post
Share on other sites
could someone please help me. I dont mean to be a pest, but I have to go soon and this is really bothering me. I know when you do this with a single dimension array you just overload the [] operator like this....

class matrix //example does not really represent a matrix now
{
public:
matrix(int Width) : width(Width) { data = new int[width]; }

//other stuff
//just return the address of data[x]
int &operator [](int x) { return data[x]; }

private:
int *data; //pointer to a dynamically allocated array
int width;
};

then you could say

matrix someMatrix(5);

//later on....
cout << someMatrix[3] << endl;

please help, and if im doing something wrong here then please tell me.

thanks,
skitzo_smurf

Share this post


Link to post
Share on other sites
    
class matrix
{
public:
matrix(int w, int h) : width(w), height(h)
{
data = new (int*)[ width ];
for (int i=0;i<width;i++)
data[ i ] = new int[ height ];
}

int* operator [](int x)
{
return data[ x ];
}

private:
int **data;
int width, height;
};


there!
matrix[ i ] return an int* as the result, and that int* is equvalent to an int[] iself.
so saying matrix[ i ][ j ], first calls matrix[ i ] to get the pointer fot data and then it references to element j ([ j ]) in that array which gives you data[ i ][ j ]. get it?

----------------
- Pouya / FOO!!!
***CENSORED***

Share this post


Link to post
Share on other sites
I don''t remember if this is legal, but give it a shot:

int matrix::operator[] ( int row, int col )

By the way you might want to have the overloaded [] operator return a reference to the int. That way you could do this:

matrix[x][y] = z;

just a thought =)
-RWarden (roberte@maui.net)

Share this post


Link to post
Share on other sites
quote:
Original post by skitzo_smurf

thank you very much for the help pouya! I will try immediately.
btw how do you put code in a window like that on this forum?

skitzo_smurf


put it in [ source ] and [ /source ] tags (don''t put the spaces there btw)

----------------
- Pouya / FOO!!!
***CENSORED***

Share this post


Link to post
Share on other sites