home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13297 < prev    next >
Encoding:
Internet Message Format  |  1992-09-04  |  1.6 KB

  1. Path: sparky!uunet!UB.com!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: delete this???
  5. Message-ID: <rmartin.715616535@thor>
  6. Date: 4 Sep 92 14:22:15 GMT
  7. References: <20146@plains.NoDak.edu>
  8. Sender: news@Rational.COM
  9. Lines: 36
  10.  
  11. heilig@plains.NoDak.edu (Zachary Heilig) writes:
  12.  
  13. |I saw the previous thread about calling the destructor in member functions,
  14. |and was wondering how legal the following code would be:
  15.  
  16.  
  17. |void foo::remove( int call_destruct )
  18. |{
  19. |    if( call_destruct ) delete this;
  20. |}
  21.  
  22.  
  23. |or, should I just call the destructor function?
  24.  
  25. |any ideas?
  26.  
  27. The syntax is correct, but the practice is ill-advised.  There are
  28. some possible ambiguities when an object deletes itself.  They have to
  29. do with the fact that the member function for that object is still
  30. executing, even though the object has been destroyed.  If any of the
  31. objects variables, or its vtbl, get accessed on the way out of the
  32. call, then trouble can ensue.  So your best bet is never to use the
  33. structure: delete this.  (And also avoid goto)
  34.  
  35. By the way, calling the destructor is also legal syntax:  foo::~foo();
  36. But the result may not be what you expect.  Calling the destructor
  37. executes the code that you wrote for the destructor, but it does not
  38. destroy the object.  Moreover, when the object is finally destroyed,
  39. the destructor will be called again.  So, take care.
  40.  
  41.  
  42. --
  43. Robert Martin                        Training courses offered in:
  44. R. C. M. Consulting                       Object Oriented Analysis
  45. 2080 Cranbrook Rd.                        Object Oriented Design
  46. Green Oaks, Il 60048 (708) 918-1004       C++
  47.