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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!brunix!brunix!sdm
  3. From: sdm@cs.brown.edu (Scott Meyers)
  4. Subject: Re: Make a deep copy!
  5. Message-ID: <1992Dec19.154329.25789@cs.brown.edu>
  6. Sender: news@cs.brown.edu
  7. Organization: Brown University Department of Computer Science
  8. References: <kwong.724744711@morgan>
  9. Date: Sat, 19 Dec 1992 15:43:29 GMT
  10. Lines: 21
  11.  
  12. In article <kwong.724744711@morgan> kwong@morgan.ucs.mun.ca (KAI S. WONG   ^.^) writes:
  13. | How about if the structure is a bit more complex, especially one with
  14. | dynamically allocated memory.  I tried the following (I appologize if it's
  15. | syntactically incorrect, I no longer have the version which I actually tried.
  16. | This one is just from memory):
  17. | class MATRIX
  18. | {
  19. ...
  20. |     MATRIX& operator+( const MATRIX& addend );
  21. | };
  22.  
  23. Your problem is that operator+ can't return a reference.  Well, it can, but
  24. if it does, it won't work correctly, as you have discovered.  The general
  25. problem is that you're returning a reference to an object that is going out
  26. of scope.  For details, see "Effective C++," pp. 82-84.
  27.  
  28. Scott
  29.  
  30. -------------------------------------------------------------------------------
  31. What do you say to a convicted felon in Providence?  "Hello, Mr. Mayor."
  32.