home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11276 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  1.4 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!uvaarpa!murdoch!virginia.edu!gs4t
  2. From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ Return Types
  5. Message-ID: <1992Jul21.215453.8138@murdoch.acc.Virginia.EDU>
  6. Date: 21 Jul 92 21:54:53 GMT
  7. References: <BrMJyE.FBM@watserv1.waterloo.edu> <1992Jul19.152207.1@cstp.umkc.edu> <23240@alice.att.com> <1992Jul21.112354.1@vax1.umkc.edu>
  8. Sender: usenet@murdoch.acc.Virginia.EDU
  9. Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  10. Organization: University of Virginia
  11. Lines: 41
  12.  
  13. kramasamy@vax1.umkc.edu writes:
  14. >ark@alice.att.com (Andrew Koenig) writes:
  15. >>     bitmap operator| (const bitmap& x, const bitmap& y)
  16. >>     {
  17. >>         bitmap result;
  18. >> 
  19. >>         // build up the result in `result', then...
  20. >> 
  21. >>         return result;
  22. >>     }
  23. >
  24. > Consider the bitmap to be of the following class definition
  25. >
  26. > class bitmap
  27. > {
  28. >     char *ptr ;
  29. >     public :
  30. >         bitmap()
  31. >         {
  32. >             ptr = new char[10] ;
  33. >         } ;
  34. >         ~bitmap()
  35. >         {
  36. >             delete [] ptr ;
  37. >         } ;
  38. > } ;
  39. >
  40. > If we adopt the second solution for the above class definition we run
  41. >into problems of deallocating 'ptr' thro' destructor and the calling routine will
  42. >get a garbage bitmap. How can we return for such cases?
  43.  
  44. The solution, in this case, is a proper copy constructor.
  45. That is, you need
  46.  
  47. bitmap::bitmap(const bitmap& b)
  48.     : ptr(new char[10])
  49. {
  50.     strcpy(ptr, b.ptr);
  51. }
  52.  
  53. -Sekar
  54.