home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: pjl@sparc13.cs.uiuc.edu (Paul Lucas)
- Subject: Re: Having this=0, class-name: :main() and tasking in c++
- Message-ID: <1992Jul27.155949.6165@sunb10.cs.uiuc.edu>
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <1DDA4ACFA5BF800C41@ursula.lucas.lu.se> <23318@alice.att.com>
- Distribution: usa
- Date: Mon, 27 Jul 1992 15:59:49 GMT
- Lines: 79
-
- In <23318@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
-
- >In article <1DDA4ACFA5BF800C41@ursula.lucas.lu.se> KOSU_MATSB@ROUTH.KOSUFY.LU.SE writes:
-
- >> Question #1: Having a 0 "this" pointer. Standard?
-
- >No. ARM page 176:
-
- > The effect of calling a nonstatic member function of a class X
- > for something that is not an object of class X is undefined.
-
- > For example,
-
- > ((X*)0)->f();
- >
- > is not guaranteed to work. In particular, this will not work for
- > virtual functions on any implementation since the information
- > needed to find the appropriate virtual function for an object
- > of type X is not found at location 0. Even for nonvirtual
- > functions, one should expect this trick to fail eventually...
-
- *****> Oops! I was in error about my previous post; sorry. But _why_
- isn't guaranteed to work for nonstatic, nonvirtual member
- functions?
-
- C++ C-code
- === ======
- ((X*)0)->f(); ==> f( /* this= */ 0 );
-
- So, for my Link class, I have:
-
- class Link {
- Link *prev, *next;
- public:
- // ...
-
- //
- // Cut() "cuts out" at most the specified number
- // of Links and returns the head of that list,
- // i.e., 'this' or null if you passed it zero.
- //
- Link* Cut( int at_most = 1 );
-
- //
- // DeleteAll() deletes all the Links in the
- // chain starting with 'this'.
- //
- void DeleteAll();
-
- //
- // Delete() deletes at most the specified number
- // of Links starting with 'this'.
- //
- void Delete( int at_most = 1 ) {
- Cut( at_most )->DeleteAll();
- }
-
- // ...
- };
-
- The thing to note is the implementation of Delete() in terms of
- Cut() and DeleteAll(); if Cut() returns null, DeleteAll() does
- nothing because the code for DeleteAll() is:
-
- void
- Link::DeleteAll() {
- for ( register Link *p = this, *q; p; p = q ) {
- q = p->next;
- delete p;
- }
- }
-
- It would seem that for cases like this, a null 'this' is
- perfectly reasonable. So why can't it be _made_ to be guaranteed
- to work?
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-