Topic: Overloading the Subscript Operator


Author: jbuck@synopsys.com (Joe Buck)
Date: 1995/07/10
Raw View
mwallman@plains.NoDak.edu (Marc Wallman) writes:
>I'm working on creating a matrix object and would like to overload the
>subscript operator ([]).  The problem is that a matrix is a 2x2 array
>of ints, so what I'd really like to do is be able to do something like
>this: matrix[2][1] = 23;  This makes my case different than all other
>examples I've seen of overloading the subscript operator.  Any suggestions
>would be appreciated.

The standard way to solve this is to have matrix::operator[](int)
return an object of a helper class, perhaps named matrix_row, and
then having matrix_row::operator[](int) return the reference to the
element.  Most C++ books describe the technique, as it's a common
question.  It's probably in the C++ FAQ as well.




--
-- Joe Buck  <jbuck@synopsys.com> (not speaking for Synopsys, Inc)
Anagrams for "information superhighway": Enormous hairy pig with fan
      A rough whimper of insanity





Author: mwallman@plains.NoDak.edu (Marc Wallman)
Date: 1995/07/07
Raw View
I'm working on creating a matrix object and would like to overload the
subscript operator ([]).  The problem is that a matrix is a 2x2 array
of ints, so what I'd really like to do is be able to do something like
this: matrix[2][1] = 23;  This makes my case different than all other
examples I've seen of overloading the subscript operator.  Any suggestions
would be appreciated.
--

Marc Wallman
mwallman@plains.nodak.edu