home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18704 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.9 KB  |  95 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!jvnc.net!newserver.jvnc.net!newsserver.technet.sg!nuscc!papaya!suresh
  3. From: suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar)
  4. Subject: Re: Overloading [] for 2d+ matrixes
  5. Message-ID: <1993Jan5.094647.7830@nuscc.nus.sg>
  6. Sender: usenet@nuscc.nus.sg
  7. Reply-To: suresh@iss.nus.sg (Suresh Thennarangam - Research Scholar)
  8. Organization: Institute Of Systems Science, NUS
  9. References: <1iarmlINN882@ub.d.umn.edu>
  10. Date: Tue, 5 Jan 1993 09:46:47 GMT
  11. Lines: 82
  12.  
  13. In article <1iarmlINN882@ub.d.umn.edu> cbusch@ub.d.umn.edu (Chris) writes:
  14. >   I been programming in C++ for quite a while now, but something
  15. >occurred to me, how can you overload [] for 2d or 3d plus matrices?
  16. >Apparently the only allowable syntax is:
  17. >   myclass::operator [] (int);
  18.  
  19. Yes, that is so but it is quite easy if you adopt the following approach:
  20.  
  21. 1> Define an operator[] function for  vector class (1 d array) which returns 
  22.     a reference to the correct element 
  23.  
  24. 2 >Your 2D matrix class is derived from this class and also has it's own
  25.    operator[] function that returns a reference to an instance of the 
  26.    vector class
  27.  
  28. so if you have
  29.  
  30. class vector
  31. {
  32. public:
  33.   vector(int) { ... };
  34.   int& operator[](int) { ...} // return appropriate data value 
  35. protected:
  36. int * data;
  37.   :
  38.   :
  39. };
  40.  
  41. class matrix: public vector
  42. {
  43. public:
  44.   matrix(int,int) { ... };
  45.   vector& operator[](int) { ...}
  46.  
  47. }
  48.  
  49. matrix  a(3,3) ;
  50.  
  51. a[2][2] = 10 ;
  52.  
  53. cout << a[2][2] ; // not sure if cout will accept a reference to an integer.
  54.                   // will someone confirm please ? 
  55.  
  56.  
  57. A simpler way would be for the matrix class to return an array which
  58. can already accept the subscript operator( i.e. []). This can be
  59. extended to higher dimensions so a matrix_3d class can return a
  60. int ** pointer which can already accept the [] operators so 
  61. you can have a statement like
  62.  
  63. class matrix_3d
  64. {
  65.  public:
  66.  int ** operator[]( int) { ....} 
  67. :
  68. :
  69. :
  70. }
  71. matrix_3d m_3d(3,3,3) ;
  72.   m_3d[2][2][2] = 0 ; 
  73.  
  74. This is rather inelegant because you can't do range checking on
  75. some of the subscripts and also you are exposing the private 
  76. data of the classes. The first method is type safe and can be
  77. nicely extended to higher dimensions( albeit a bit tedious to
  78. implement).  
  79.  
  80.  
  81. Hope that answers your question.
  82.  
  83.       __                  
  84.      (_   / /  o_   o  o |_
  85.      __)/(_( __) (_(_ /_)| )_
  86.  
  87. ***************************************************************************
  88. * Suresh Thennarangam               *  EMail: suresh@iss.nus.sg(Internet) *
  89. * Research Scholar                  *         ISSST@NUSVM.BITNET          *
  90. * Institute Of Systems Science      *  Tel:  (065) 772 2588.              *
  91. * National University Of Singapore  *  Facs.: (065) 778 2571              *
  92. * Heng Mui Keng Terrace             *  Telex: ISSNUS RS 39988             *
  93. * Singapore 0511.                   *                                     *
  94. ***************************************************************************
  95.