home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / std / cplus / 1102 < prev    next >
Encoding:
Text File  |  1992-08-21  |  4.1 KB  |  115 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc6.cs.uiuc.edu!pjl
  3. From: pjl@sparc6.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: this == 0 for nonvirtual functions
  5. Message-ID: <1992Aug22.011400.14896@sunb10.cs.uiuc.edu>
  6. Keywords: this virtual function undefined
  7. Sender: news@sunb10.cs.uiuc.edu
  8. Organization: University of Illinois at Urbana-Champaign
  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>
  10. Distribution: usa
  11. Date: Sat, 22 Aug 1992 01:14:00 GMT
  12. Lines: 101
  13.  
  14. In <23512@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
  15.  
  16. >Perhaps someone might like to try to construct an example of why
  17. >it might actually be useful to allow this==0 in a member function.
  18. >I've been away, so I've missed some messages, but so far the only
  19. >justification I've seen is statement like `it seems like a good idea.'
  20.  
  21. >I find it hard to imagine why it would be useful.  A member function
  22. >that never looks at `this' could just as well be made static;
  23. >one that actually uses `this' had better not be called with this==0.
  24. >Thus it appears that the only useful cases are:
  25.  
  26. >    1. a function that actually checks if this==0 and does different
  27. >       things depending on the result, or
  28.  
  29. >    2. a function that looks at this only if one of its arguments has
  30. >       a particular value.
  31.  
  32. >Both of these cases seem contrived to me, to the extent that I would
  33. >expect to be able to accomplish an equivalent thing more elegantly by
  34. >other means.
  35.  
  36. >Can someone actually come with a useful example?
  37.  
  38.     My original example is for classes whose objects are dealt with
  39.     almost exclusively via pointer, e.g., canonical linked-list
  40.     classes or other container classes that use linked-lists for
  41.     their implementation.
  42.  
  43.     The example I gave was the member-function:
  44.  
  45.         void Link::DeleteAll() {
  46.             for ( Link *p = this, *q; p; p = q ) {
  47.                 q = p->next;
  48.                 delete p;
  49.             }
  50.         }
  51.  
  52.     where the Link class is what you'd expect, i.e.:
  53.  
  54.         class Link {
  55.             Link *prev, *next;
  56.             // ...
  57.         };
  58.     
  59.     The DeleteAll() member, if pass a nil pointer, would (should) do
  60.     nothing because its this == 0 to start out.
  61.  
  62.     Also consider:
  63.  
  64.         Link* Link::Cut( int how_many = 1 ) {
  65.             // "cut out" how_many links starting at this
  66.             // and return a pointer to the new sub-list.
  67.             // If how_many == 0, return 0
  68.         }
  69.  
  70.         void Link::Delete( int how_many = 1 ) {
  71.             Cut( how_many )->DeleteAll();
  72.             // Note that if Cut() returns nil, it would be
  73.             // if calls where this == 0 were guaranteed to
  74.             // work.
  75.         }
  76.     
  77.     There are also many other examples along similar lines where not
  78.     having to check for this == 0 would be nice.  Clearly, these
  79.     members can not be static.
  80.  
  81.     To reiterate (for Andrew's sake), I also think that one of the
  82.     _themes_ in C++ is to lessen or eliminate tedium and errors
  83.     introduced thereby, e.g., virtual functions eliminate missing
  84.     cases in swith statements, exception-handling (when it gets
  85.     here) to eliminate explicit checks for nil pointers returned by
  86.     new() (among other things), etc.  I seem my (modest) proposal in
  87.     the same light.
  88.  
  89.     Again, to reiterate (for Andrew's sake), I think it's a
  90.     zero-cost feature; code that works now will continue to work,
  91.     and programmers who ignore it will be unaffected, i.e., if they
  92.     don't check 'this' their programs will exhibit undefined
  93.     behavior (by trying to access members) and "undefined behavior"
  94.     is what happens now anyway.
  95.  
  96.     If nothing else, I would at _least_ like those two seemingly
  97.     contradictory paragraphs in the ARM explained, i.e., the ones
  98.     that say:
  99.  
  100.         ...calling a non-virtual member-function for something
  101.         that is not an object should be expected to fail
  102.         eventually... (or something along those lines)
  103.     and:
  104.         Naturally, this trick would work if the member were to
  105.         check its this pointer before accessing any members...
  106.     
  107.     Does the latter sentence mean that it really _is_ ok to make the
  108.     _call_ with a this == 0, i.e., do I already have what I want?
  109.  
  110.     Thanks in advance.
  111. -- 
  112.     - Paul J. Lucas                University of Illinois    
  113.       AT&T Bell Laboratories        at Urbana-Champaign
  114.       Naperville, IL            pjl@cs.uiuc.edu
  115.