matrix operator overloading

Started by
4 comments, last by skitzo_smurf 23 years, 9 months ago
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
Advertisement
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
    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 return an int* as the result, and that int* is equvalent to an int[] iself.<br>so saying matrix[ j ], first calls matrix to get the pointer fot data and then it references to element j ([ j ]) in that array which gives you data[ j ]. get it? <br><br><font color="red">—————-</font><br><font color="yellow">- Pouya / FOO!!! </font><br><font color="blue">***CENSORED***</font>
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
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)
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***

This topic is closed to new replies.

Advertisement