skinner@saturn.ucsc.edu (Robert Skinner) (10/20/88)
(I know that this subject was discussed a few weeks ago, but I didn't pay much attention then. Now I need to know. Sorry for the repetition.) I'm constructing a general 2d Matrix object for a class I'm now taking on matrix computations. It would be nice if I could overload the [] operator to allow something like a[1][2], but I understand that is not possible. Another option is to overload the () operator to provide array indexing: virtual float *operator()( int i, int j ) { return data + i*cols + j; } This works, but it proliferates lots of *'s in the code, making it less readable. Is there something I'm missing that was explained by a previous poster? Or is this the best that can be done right now. Robert Skinner skinner@saturn.ucsc.edu
baud@gt-eedsp.UUCP (Kurt Baudendistel) (10/20/88)
In article <5181@saturn.ucsc.edu> skinner@saturn.ucsc.edu (Robert Skinner) writes: >I'm constructing a general 2d Matrix object for a class I'm now taking >on matrix computations. It would be nice if I could >overload the [] operator to allow something like a[1][2], but I >understand that is not possible. you should understand that you CAN do this if you want: 1. a is of type MATRIX. 2. define MATRIX::operator[] to return a value of type VECTOR. 3. define VECTOR::operator[] to return a value of type float. the concept is simple. the details are tricky. >Another option is to overload the () operator to provide array indexing: > > virtual float *operator()( int i, int j ) > { return data + i*cols + j; } > i don't really understand where ``all the *'s in the code'' come from, but what you really want to do is return a reference to the value: float& operator()( int i, int j ) { return *(data + i*cols + j); } even better, watch for the prototypable matrix class to be included soon in the libg++ distribution (it comes with g++, but could be adapted by someone for general c++ use). kurt -- Kurt Baudendistel [GRA McClellan] Georgia Tech, School of Electrical Engineering, Atlanta, GA 30332 USENET: ...!{allegra,hplabs,ihnp4,ulysses}!gatech!gt-eedsp!baud INTERNET: gt-eedsp!baud@gatech.edu
mtoy@xman.SGI.COM (Michael Toy -- The S.G.I. XMAN) (10/21/88)
In article <5181@saturn.ucsc.edu>, (Robert Skinner) writes: > I'm constructing a general 2d Matrix object for a class I'm now taking > on matrix computations. It would be nice if I could > overload the [] operator ... > --------------------------- File Matrix.h -------------------------------- // // Generic class for 2D matrices of any type with runtime sizing // #define MatrixDeclare(type) \ class MatrixOf/**/type {\ type* data;\ int ncols; \ public:\ MatrixOf/**/type() \ { data = 0; } \ MatrixOf/**/type(int rows, int cols) \ { ncols = cols; data = new type[rows * cols]; } \ type* operator[](int row)\ { return data + row*ncols; }\ ~MatrixOf/**/type()\ { if (data) delete data; }\ } #define Matrix(type) MatrixOf/**/type --------------------------- End File main.c++ -------------------------------- To define a type for a matrix of "FROB"'s, just do: #include "Matrix.h" MatrixDeclare(FROB) To get a m by n matrix of FROBS do: Matrix(FROB) my_matrix(m, n); now both "my_frob = my_matrix[2][3]" and "my_matrix[2][3] = my_frob" should work. -- From the mixed up files of Mr. Michael C. Toy Internet: mtoy@SGI.COM UUCP: {ames,ucbvax,decwrl,sun,parcvax}!mtoy