home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / std / cplus / 1104 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  2.4 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: this == 0 for nonvirtual functions
  5. Keywords: this virtual function undefined
  6. Message-ID: <23528@alice.att.com>
  7. Date: 22 Aug 92 15:52:58 GMT
  8. Article-I.D.: alice.23528
  9. References: <1992Aug18.045605.14220@sunb10.cs.uiuc.edu> <1992Aug20.215455.17279@microsoft.com> <1992Aug21.044448.8282@sunb10.cs.uiuc.edu> <23512@alice.att.com> <1992Aug22.011400.14896@sunb10.cs.uiuc.edu>
  10. Reply-To: ark@alice.UUCP ()
  11. Distribution: usa
  12. Organization: AT&T Bell Laboratories, Murray Hill NJ
  13. Lines: 58
  14.  
  15. In article <1992Aug22.011400.14896@sunb10.cs.uiuc.edu> pjl@sparc6.cs.uiuc.edu (Paul Lucas) writes:
  16.  
  17. >     The example I gave was the member-function:
  18.  
  19. >         void Link::DeleteAll() {
  20. >             for ( Link *p = this, *q; p; p = q ) {
  21. >                 q = p->next;
  22. >                 delete p;
  23. >             }
  24. >         }
  25.  
  26. Because this example ultimately says `delete this' (because p == this
  27. and it says `delete p'), I am already tempted to dismiss it out of hand.
  28. I think it's a Bad Idea to write member functions that invalidate their
  29. object, because it is so far away from what lots of people will expect.
  30.  
  31. Putting that aside, though, I don't see why it's any harder to make DeleteAll
  32. a static member function with an explicit pointer argument:
  33.  
  34.     /* static */ void Link::DeleteAll(Link* p) {
  35.         for (Link* q; p; p = q) {
  36.             q = p->next;
  37.             delete p;
  38.         }
  39.     }
  40.  
  41. The code is about the same length; the only difference in use is that
  42. instead of saying
  43.  
  44.     foo->DeleteAll();
  45.  
  46. you say
  47.  
  48.     Link::DeleteAll(foo);
  49.  
  50. So I don't think this example is a particularly telling argument.
  51.  
  52. Ditto for the next example (omitted for brevity).
  53.  
  54. >     Again, to reiterate (for Andrew's sake), I think it's a
  55. >     zero-cost feature; code that works now will continue to work,
  56.  
  57. It is not a zero-cost feature, because it would require compilers to
  58. generate extra code for every call of a member function inherited
  59. from a base class in the presence of virtual inheritance.
  60.  
  61. >     and programmers who ignore it will be unaffected, i.e., if they
  62. >     don't check 'this' their programs will exhibit undefined
  63. >     behavior (by trying to access members) and "undefined behavior"
  64. >     is what happens now anyway.
  65.  
  66. The feature would prohibit C++ implementations from detecting this==0
  67. as an error, which would make at least some genuine mistakes harder to find.
  68.  
  69.  
  70. -- 
  71.                 --Andrew Koenig
  72.                   ark@europa.att.com
  73.