home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15992 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  2.6 KB

  1. Path: sparky!uunet!sun-barr!decwrl!pacbell.com!network.ucsd.edu!sdcc12!cs!pluto
  2. From: pluto@cs.ucsd.edu (Mark Plutowksi)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: operator [][]
  5. Message-ID: <40779@sdcc12.ucsd.edu>
  6. Date: 9 Nov 92 19:03:38 GMT
  7. References: <BxFt8r.2Kt@cs.columbia.edu> <1992Nov9.095352.19114@fmrco.uucp>
  8. Sender: news@sdcc12.ucsd.edu
  9. Lines: 92
  10. Nntp-Posting-Host: beowulf.ucsd.edu
  11.  
  12. pandrews@lovat.fmrco.com (Paul Andrews) writes:
  13.  
  14.  
  15. >> for multiple dimension array, is it possible to define 
  16. >> an operator [][](say for two dimensions)? I try to define 
  17. >> it as 
  18. >> int & operator[][] (int, int);
  19. >> 
  20. >You can only overload single []. Remember though that this generates a function call
  21. >like:
  22. >    <class>.operator[](<arg>)
  23.  
  24. >which returns a value. So make that value be a reference to another class which also
  25. >has an operator[] overload. Then when you specify:
  26.  
  27. >    <classA>[<arg1>][<arg2>]
  28.  
  29. >you will actually get
  30.  
  31. >    <classA>.operator[](<arg1>).<classB>.operator[](<arg2>)
  32.  
  33. >classB should now call a well-known function in classA to indicate what argument was
  34. >passed to it. A good way of doing this is for classA to inherit yet another class
  35. >(classC!) which has a well-known virtual function which classA overrides. That way
  36. >both classC and classB can be re-used whenever you want to have multiple [].
  37.  
  38. >Simple huh?
  39.  
  40. >(apologies if the syntax of the expanded function calls isn't quite correct).
  41.  
  42. >(I would be interested to know if anyone has any other ways of doing this).
  43.  
  44. >(Is there any standard nomenclature for classes like C and B above? something like
  45. >classB is a slave class, classC is a functor or what?)
  46.  
  47. >---
  48. >---
  49. >Paul Andrews
  50.  
  51.  
  52. I think that what I did is another way:
  53. I have a Matrix class that contains a data member which is 
  54. an array of pointers to Vectors, where Vector is another class, whose
  55. primary data member is an array of pointers to some number type (double, int, ...)  
  56. Let aVec be a vector.  Then, aVec[i] gives its i-th element, (because I specified
  57. the [] operator as above.)  So, I just give the Matrix class the [] 
  58. operator as well, and voila, aMatrix[][] gives me what I want -
  59. which I gather is also what you're trying to do.
  60. It does, however, distribute the internal storage of your array
  61. (whereas as I understand it the above solution retains the array
  62. as a single object referenced by two classes using two access functions)
  63. In my case, this was desirable, because splitting up large arrays
  64. into small Vectors grouped into a Matrix allowed me to use arrays much
  65. larger than the 64K segments my machine constrains me to. 
  66.  
  67. .
  68. .
  69. .
  70. .
  71. .
  72. .
  73. .
  74. .
  75. i
  76.  
  77. have
  78.  
  79. no
  80.  
  81. signature
  82.  
  83.  
  84. please 
  85.  
  86.  
  87. let 
  88.  
  89.  
  90. this
  91.  
  92.  
  93. suffice
  94.  
  95. Mr.
  96.  
  97. Auto
  98.  
  99. reject
  100.  
  101. Program
  102.  
  103. Sir
  104.