home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15958 < prev    next >
Encoding:
Text File  |  1992-11-09  |  1.7 KB  |  53 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!fmrco!pandrews!pandrews
  3. From: pandrews@lovat.fmrco.com (Paul Andrews)
  4. Subject: Re: operator [][]
  5. Message-ID: <1992Nov9.095352.19114@fmrco.uucp>
  6. Sender: news@fmrco.uucp
  7. Reply-To: pandrews@lovat.fmrco.com
  8. Organization: Fidelity Investments Ltd.
  9. References: <BxFt8r.2Kt@cs.columbia.edu>
  10. Date: Mon, 9 Nov 1992 09:53:52 GMT
  11. Lines: 40
  12.  
  13. In article 2Kt@cs.columbia.edu, duanyang@cs.columbia.edu (Duanyang Guo) writes:
  14. > for multiple dimension array, is it possible to define 
  15. > an operator [][](say for two dimensions)? I try to define 
  16. > it as 
  17. > int & operator[][] (int, int);
  18. > which g++ complains about it. 
  19. > any idea or just sth is wrong with my try? 
  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.