home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18353 < prev    next >
Encoding:
Text File  |  1992-12-23  |  1.6 KB  |  43 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Is it possible to use 'delete this' ?
  5. Message-ID: <rmartin.725129268@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <Dy93VB2w165w@bwhwob.escape.de>
  9. Date: Wed, 23 Dec 1992 16:47:48 GMT
  10. Lines: 31
  11.  
  12. bernd@bwhwob.escape.de (Bernd Wiegmann) writes:
  13.  
  14. >Hi,
  15.  
  16. >Is it possible to use 'delete this' as the last statement in a
  17. >method of a class ?
  18.  
  19. In general, "delete this" is a bad idea.  I don't beleive you can
  20. depend upon all the compiler vendors to generate code which is immune
  21. to losing the object before the methods are complete.  Also, you run
  22. into difficulties if the object was not allocated with new, or if you
  23. overload new and delete and then allocate an array of objects.
  24.  
  25. In general, an object should not be privy to the method used to
  26. allocate it.  It is the user of the object that decides whether it
  27. should be dynamic, auto or static.  There is no good way for an object
  28. to detect which of these allocation methods were used.  Morover, even
  29. if you are absolutely certain that the object is dynamically
  30. allocated, you still can't be absolutely sure which dynamic allocation
  31. method was used.  Arrays of objects get deleted in a different way
  32. than individual objects.  You cannot allocate an array and then delete
  33. the individuals.
  34.  
  35. So, in short, avoid "delete this" where possible.
  36.  
  37.  
  38. --
  39. Robert Martin                        Training courses offered in:
  40. R. C. M. Consulting                       Object Oriented Analysis
  41. 2080 Cranbrook Rd.                        Object Oriented Design
  42. Green Oaks, Il 60048 (708) 918-1004       C++
  43.