home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13709 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  3.3 KB

  1. Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Proposal: Subscripting with multiple arguments
  5. Message-ID: <TMB.92Sep15223337@arolla.idiap.ch>
  6. Date: 16 Sep 92 02:33:37 GMT
  7. References: <1992Sep14.182942.1986@athena.mit.edu>
  8.     <MATT.92Sep14161955@physics.berkeley.edu>
  9. Sender: news@ai.mit.edu
  10. Reply-To: tmb@idiap.ch
  11. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  12.     Perceptive)
  13. Lines: 51
  14. In-reply-to: matt@physics.berkeley.edu's message of 14 Sep 92 23:19:55 GMT
  15.  
  16. In article <MATT.92Sep14161955@physics.berkeley.edu> matt@physics.berkeley.edu (Matt Austern) writes:
  17.  
  18.    In article <1992Sep14.182942.1986@athena.mit.edu> mrhagger@athena.mit.edu (Michael R Haggerty) writes:
  19.  
  20.    > I have been using C++ for numerical work, and one of the changes which would
  21.    > be most useful would be to allow the subscription operator to take multiple
  22.    > parameters.  This would make it much simpler to define classes for multi-
  23.    > dimensional matrices without the nuisance of having to define a helper class.
  24.    > The concept should not be such a shock to anyone, as the subscript operator
  25.    > is already allowed to take non-integer arguments so is already significantly
  26.    > extended from the C subscript operator.
  27.  
  28.    I've also occasionally wished that I could define, say, 
  29.        operator[](int,int,int), 
  30.    and for the same reason: multi-dimensional arrays.  I haven't wished
  31.    for it very often, though, because that isn't really the syntax that I
  32.    want for a multi-dimensional array anyway; I would much rather access
  33.    an array element using the syntax A[i][j] than using the syntax
  34.    A[i,j].  After all, the A[i][j] syntax is what you would use if you
  35.    were using built-in data types instead of classes, and it's nice to
  36.    keep some things the same when defining a class.  (For that matter,
  37.    there isn't really anything wrong with the syntax A(i,j), either.)
  38.  
  39. I have been using operator()(int,int,...) exclusively for
  40. multidimensional arrays, and it works nicely. Many other languages
  41. use this notation, and some even consider arrays simply a special
  42. kind of function anyway.
  43.  
  44.    What I find to be a bigger problem, actually, is designing an array
  45.    class that works for arrays with an arbitrary number of dimensions.
  46.    There's no problem with defining the classes Array1, Array2,
  47.    Array3,..., and that's exactly what I've done, but it's annoying and
  48.    inelegant.
  49.  
  50. I don't think it is inelegant. After all, a 1D array and a 2D array
  51. are very different kinds of objects. Why should they have the same
  52. type? You would lose some of the advantages of type checking that way.
  53.  
  54. Also, I have found that I rarely need arrays of dimension higher than
  55. 3 anyway, since most arrays either represent regularly spaced values
  56. in 1D, 2D, or 3D Euclidean space, or a sequence of objects indexed by
  57. intervals over the integers. Most uses of higher dimensional arrays
  58. really tend to be collections of lower-dimensional arrays, which are
  59. more naturally represented as such (e.g., Array< Array< Array2<float> > >).
  60.  
  61. What you could do, however, is to share a lot of code among the
  62. different array classes. For example, you might have your
  63. multidimensional array inherit from your 1D array. Still, I have found
  64. it easier to write separate 1D, 2D, and 3D array template classes.
  65.  
  66.                     Thomas.
  67.