home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11254 < prev    next >
Encoding:
Text File  |  1992-07-21  |  1.6 KB  |  72 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!kuhub.cc.ukans.edu!parsifal.umkc.edu!vax1.umkc.edu!kramasamy
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: C++ Return Types
  4. Message-ID: <1992Jul21.112354.1@vax1.umkc.edu>
  5. From: kramasamy@vax1.umkc.edu
  6. Date: Tue, 21 Jul 1992 17:23:54 GMT
  7. Sender: root@parsifal.umkc.edu (Parsifal Administration)
  8. References: <BrMJyE.FBM@watserv1.waterloo.edu> <1992Jul19.152207.1@cstp.umkc.edu> <23240@alice.att.com>
  9. Organization: University of Missouri - Kansas City
  10. Lines: 60
  11.  
  12. In article <23240@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
  13. > In article <1992Jul19.152207.1@cstp.umkc.edu> pkailasam@cstp.umkc.edu writes:
  14. >>     bitmap &bitmap::operator |(const bitmap &bm)
  15. >>     {
  16. >>         bitmap *bm1 = new bitmap ;
  17. >>         
  18. >>         // Do the necessary operations in the bitmap pointer bm1
  19. >> 
  20. >>         return *bm1 ;
  21. >>     }   
  22. > Thereby allocating a bitmap every time the function is called
  23. > and never freeing it.  How about the obvious approach:
  24. >     bitmap operator| (const bitmap& x, const bitmap& y)
  25. >     {
  26. >         bitmap result;
  27. >         // build up the result in `result', then...
  28. >         return result;
  29. >     }
  30. > -- 
  31. >                 --Andrew Koenig
  32. >                   ark@europa.att.com
  33.  
  34.     Consider the bitmap to be of the following class definition
  35.  
  36.     class bitmap
  37.     {
  38.         ...
  39.  
  40.         char *ptr ;
  41.  
  42.         public :
  43.  
  44.             bitmap()
  45.             {
  46.                 ...
  47.                 ptr = new char[10] ;
  48.             } ;
  49.  
  50.  
  51.             ~bitmap()
  52.             {
  53.                 delete [] ptr ;
  54.             } ;
  55.     } ;
  56.  
  57.     If we adopt the second solution for the above class definition we run
  58. into problems of deallocating 'ptr' thro' destructor and the calling routine will
  59. get a garbage bitmap. How can we return for such cases?
  60.  
  61.  
  62. -karthik
  63.  
  64. karthik@vax1.umkc.edu
  65.  
  66.  
  67.