home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11605 < prev    next >
Encoding:
Text File  |  1992-07-27  |  2.6 KB  |  92 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wupost!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc13.cs.uiuc.edu!pjl
  3. From: pjl@sparc13.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: Having this=0, class-name: :main() and tasking in c++
  5. Message-ID: <1992Jul27.155949.6165@sunb10.cs.uiuc.edu>
  6. Sender: news@sunb10.cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <1DDA4ACFA5BF800C41@ursula.lucas.lu.se> <23318@alice.att.com>
  9. Distribution: usa
  10. Date: Mon, 27 Jul 1992 15:59:49 GMT
  11. Lines: 79
  12.  
  13. In <23318@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
  14.  
  15. >In article <1DDA4ACFA5BF800C41@ursula.lucas.lu.se> KOSU_MATSB@ROUTH.KOSUFY.LU.SE writes:
  16.  
  17. >> Question #1:    Having a 0 "this" pointer. Standard?
  18.  
  19. >No.  ARM page 176:
  20.  
  21. >    The effect of calling a nonstatic member function of a class X
  22. >    for something that is not an object of class X is undefined.
  23.  
  24. >    For example,
  25.  
  26. >        ((X*)0)->f();
  27. >    
  28. >    is not guaranteed to work.  In particular, this will not work for
  29. >    virtual functions on any implementation since the information
  30. >    needed to find the appropriate virtual function for an object
  31. >    of type X is not found at location 0.  Even for nonvirtual
  32. >    functions, one should expect this trick to fail eventually...
  33.  
  34. *****>    Oops!  I was in error about my previous post; sorry.  But _why_
  35.     isn't guaranteed to work for nonstatic, nonvirtual member
  36.     functions?
  37.  
  38.         C++            C-code
  39.         ===            ======
  40.         ((X*)0)->f();    ==>    f( /* this= */ 0 );
  41.     
  42.     So, for my Link class, I have:
  43.  
  44.         class Link {
  45.             Link *prev, *next;
  46.         public:
  47.             // ...
  48.  
  49.             //
  50.             // Cut() "cuts out" at most the specified number
  51.             // of Links and returns the head of that list,
  52.             // i.e., 'this' or null if you passed it zero.
  53.             //
  54.             Link* Cut( int at_most = 1 );
  55.  
  56.             //
  57.             // DeleteAll() deletes all the Links in the
  58.             // chain starting with 'this'.
  59.             //
  60.             void DeleteAll();
  61.  
  62.             //
  63.             // Delete() deletes at most the specified number
  64.             // of Links starting with 'this'.
  65.             //
  66.             void Delete( int at_most = 1 ) {
  67.                 Cut( at_most )->DeleteAll();
  68.             }
  69.  
  70.             // ...
  71.         };
  72.  
  73.     The thing to note is the implementation of Delete() in terms of
  74.     Cut() and DeleteAll(); if Cut() returns null, DeleteAll() does
  75.     nothing because the code for DeleteAll() is:
  76.  
  77.             void
  78.         Link::DeleteAll() {
  79.             for ( register Link *p = this, *q; p; p = q ) {
  80.                 q = p->next;
  81.                 delete p;
  82.             }
  83.         }
  84.     
  85.     It would seem that for cases like this, a null 'this' is
  86.     perfectly reasonable.  So why can't it be _made_ to be guaranteed
  87.     to work?
  88. -- 
  89.     - Paul J. Lucas                University of Illinois    
  90.       AT&T Bell Laboratories        at Urbana-Champaign
  91.       Naperville, IL            pjl@cs.uiuc.edu
  92.