home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18328 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  2.3 KB

  1. Path: sparky!uunet!gatech!usenet.ins.cwru.edu!agate!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Make a deep copy!
  5. Message-ID: <769@ulogic.UUCP>
  6. Date: 22 Dec 92 22:41:34 GMT
  7. References: <kwong.724744711@morgan>
  8. Organization: negligable
  9. Lines: 49
  10.  
  11. In article <kwong.724744711@morgan> kwong@morgan.ucs.mun.ca (KAI S. WONG   ^.^) writes:
  12. :
  13. :
  14. :In article <1992Dec11.232954.7071@borland.com>, pete@borland.com (Pete Becker) writes:
  15. :>> In article <1992Dec11.170849.18525@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Guy Pickering) writes:
  16. :>> >In general, can I create an instance of a class in a member function and
  17. :>> >return the object? Doesn't the complex object go out of scope?
  18. :>> 
  19. :>>     Yes, it does.  But that's not the end of the inquiry.
  20. :>> 
  21. :>>     int add( int a, int b )
  22. :>>     {
  23. :>>     return a+b;
  24. :>>     }
  25. :>> 
  26. :>>     Here, too, the temporary goes out of scope, but the compiler knows how
  27. :>> to deal with that.  Same thing for an object of a user-defined type.
  28. :>> 
  29. :
  30. :How about if the structure is a bit more complex, especially one with
  31. :dynamically allocated memory.  I tried the following (I appologize if it's
  32. :syntactically incorrect, I no longer have the version which I actually tried.
  33. :This one is just from memory):
  34. :
  35.     [ ... example deleted ... ]
  36. :
  37. :the "+" operation works and sum contains the proper matrix, that is until
  38. :"+" finishes and goes out of scope.  The "return" statement makes a temporary
  39. :copy of sum (ie. the values of its nrows, ncols, and the pointer entry), and
  40. :then proceed to destroy sum automatically.  So, now the entry pointer of this
  41. :temporary is no longer pointing to a valid block of memory!!   
  42. :
  43. :Is this really what happened?  I am new to C++, I am not sure my interpretation
  44. :is correct.  But all I can tell is that this simple addition and assignment 
  45. :sequence DOESN'T work, and I don't know how to get around it.  HELP!!
  46.  
  47. I believe that these are the situations that indicate that you 
  48. need to have a copy constructor and the operator=() defined for 
  49. your class.
  50.  
  51. I'm sure someone will correct me if I'm wrong.        :)
  52.  
  53.  
  54.         -Richard Hartman
  55.         hartman@uLogic.COM
  56.  
  57. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  58. "Ideas are not responsible for the people who believe them."
  59.  
  60.