Skip to main content
gamedev.net
🔒 Locked

matrix operator overloading

Started by skitzo_smurf Jul 11, 2000 at 4:36 PM 4 replies 250+ views
Original Post
skitzo_smurf
skitzo_smurf
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
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
skitzo_smurf
skitzo_smurf
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
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
Pouya
Pouya
    
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***
skitzo_smurf
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
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
RWarden
RWarden
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)
Pouya
Pouya
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***

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.