Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!gatech!gt-eedsp!baud From: baud@gt-eedsp.UUCP (Kurt Baudendistel) Newsgroups: comp.lang.c++ Subject: Re: matrix types and [] overloading Summary: a supportive response Keywords: matrix, [] overloading Message-ID: <538@gt-eedsp.UUCP> Date: 20 Oct 88 15:17:50 GMT References: <5181@saturn.ucsc.edu> Reply-To: baud@gt-eedsp.UUCP (Kurt Baudendistel) Organization: School of Electrical Engineering, Ga. Tech, Atlanta, GA 30332 Lines: 35 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