home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18239 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.1 KB  |  54 lines

  1. Newsgroups: uw.dcs.general,comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Operator Overloading problem
  5. Message-ID: <1992Dec19.182648.11687@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1992Dec16.192756.13271@dcs.warwick.ac.uk>
  10. Date: Sat, 19 Dec 1992 18:26:48 GMT
  11. Lines: 41
  12.  
  13. In article <1992Dec16.192756.13271@dcs.warwick.ac.uk> jas@dcs.warwick.ac.uk (Jason Morris) writes:
  14. >
  15. >  I have a class, Matrix, and I want to overload the operators +,-,* etc.
  16. >Matrix has a private member Data, which is a pointer to some dynamically
  17. >allocated storage. Matrix() allocates this storage, and ~Matrix() frees it.
  18. >Fine so far.
  19.  
  20.     No. No good. You MUST also have a copy constructor and assignment
  21. operator that copy the contents of Data, and delete and copy its contents,
  22. respectively.
  23.  
  24.     At least initially, forget about the inefficieny, fix that later.
  25. This technique is called the 'Orthodox Idiom' by Coplien, it 
  26. ensures the objects are first class 'just like int'. Coplien
  27. provides the full recipe, follow it slavishly.
  28.  
  29. >
  30. >    friend Matrix operator+(Matrix& lhs,Matrix& rhs);
  31.  
  32.     friend Matrix operator+(const Matrix& lhs, const Matrix& rhs);
  33.     //                      ^^^^^
  34.     //   if you want to pass it expressions
  35. >
  36. >  And I just do "return <local Matrix>"
  37. >
  38.  
  39.     And now it works. To make it more efficient, there are
  40. lots of methods, Coplien is filled with them. Some matrix 
  41. packages go so far as to just build a 'parse tree' of the
  42. calculation rather than doing it straight away, to optimise
  43. speed and memory use [defered binding]
  44.  
  45.     However, you should be able to make your package
  46. more efficient without having to rewrite existing working
  47. client code (although you might have to recompile).
  48.  
  49. -- 
  50. ;----------------------------------------------------------------------
  51.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  52.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  53. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  54.