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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Do temps hurt performance of overloaded +
  5. Message-ID: <1992Dec17.210357.8792@microsoft.com>
  6. Date: 17 Dec 92 21:03:57 GMT
  7. Organization: Microsoft Corporation
  8. References: <1gofq7INN8q@golden.kaleida.com>
  9. Lines: 22
  10.  
  11. In article <1gofq7INN8q@golden.kaleida.com> gordie@duts.ccc.amdahl.com writes:
  12. |If I do the following to overload binary + for a class, will temporary
  13. |objects be created? And if so, what can I do to prevent this, to get the best
  14. |performance?
  15. ....
  16. |inline Complex operator + (Complex& x, Complex& y)
  17. |{
  18. |    return Complex (x.real + y.real, x.imaginary + y.imaginary);
  19. |} 
  20.  
  21. This of course is all very implementation dependent, but your approach
  22. works well with my compiler, not only avoiding temporaries, but
  23. actually resulting in compile-time evaluation of your
  24. expressions involving stack variables.
  25.  
  26. In general where you pass-by-reference you should be passing by
  27. const ref:
  28.  
  29. inline Complex operator + (const Complex& x, const Complex& y)
  30.  
  31. to indicate the procedure does not modify those referenced.
  32.  
  33.