home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 19003 < prev    next >
Encoding:
Text File  |  1993-01-12  |  3.2 KB  |  69 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!Germany.EU.net!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: Is it possible to use 'delete this' ?
  5. In-Reply-To: rmartin@thor.Rational.COM's message of Sun, 10 Jan 1993 14:12:48 GMT
  6. Message-ID: <KANZE.93Jan12153048@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <rmartin.725129268@thor> <BzqE5s.6Lr@comp.vuw.ac.nz> <rmartin.725581109@thor>
  10.     <C000qE.D3J@comp.vuw.ac.nz> <rmartin.726675168@thor>
  11. Date: 12 Jan 93 15:30:48
  12. Lines: 55
  13.  
  14. In article <rmartin.726675168@thor> rmartin@thor.Rational.COM (Bob
  15. Martin) writes:
  16.  
  17. |> robertd@kauri.vuw.ac.nz (Robert Davies) writes:
  18.  
  19. |> |The problem I had with Bob's first reply was that he didn't answer the
  20. |> |question: is "delete this" legal.
  21.  
  22. |> To my knowledge, the syntax is legal.  (Steve Clamage:  Can you
  23. |> comment on legality?)
  24.  
  25. |> |Then it would be appropriate to give a warning. Incidentally, is the warning
  26. |> |about is causing difficulty to some compilers just a "gut feeling" or are
  27. |> |there specific examples?
  28.  
  29. |> No specific examples.  I give the warning because I have seen no words
  30. |> in the ARM to the effect that the code generated by the compiler must
  31. |> assume that the object may not exist during a member function call.
  32.  
  33. |> |Bob's warning about "delete this" seems to apply to any use of "delete" by
  34. |> |an entity that didn't do the corresponding "new". Otherwise there is always
  35. |> |the risk that the "delete" will be applied to something not created by
  36. |> |"new". Is this another safe programming rule?
  37.  
  38. |> No.  And my warning is only about the dangers of "delete this".
  39. |> "delete this" can only be appropriate for objects that are "always"
  40. |> allocated from the heap.  Unfortunately, there is no way for an object
  41. |> to insure this. 
  42.  
  43. Granted, there is *no* way for an object alone to ensure that it was
  44. allocated on the heap.  But not all classes exist in isolation.  If a
  45. class is declared in the private part of another class, then it can
  46. only be used by the containing class.  In this situation, the author
  47. of the *two* classes *can* control where all of the instances of the
  48. contained class come from, and using "delete this" would seem in this
  49. case to be a reasonable solution.
  50.  
  51. As a practical example, a tree class contains the definition of its
  52. nodes.  The node class(es) themselves contain a cleanup function,
  53. which calls cleanup on the children, and then delete this.  The
  54. destructor for the tree simply calls cleanup on the root node.  (I
  55. have actually used code like this, and find it quite practical.)
  56.  
  57. On the other hand, I am very sceptical of using delete this in a class
  58. which is directly accessible to everyone, for the very reason that Bob
  59. states.  If a user can do something, you must program under the
  60. assumption that he will, and if the class is accessable to the user,
  61. he can (and sooner or later will) declare an object of that class on
  62. the stack.  So I would consider it very bad programming style to use
  63. "delete this" in any such class.
  64. --
  65. James Kanze                             email: kanze@us-es.sel.de
  66. GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
  67. Conseils en informatique industrielle --
  68.                    -- Beratung in industrieller Datenverarbeitung
  69.