🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #144

Re: this == 0 for nonvirtual functions

#144
From: Andrew Koenig
Date: Sat, 22 Aug 1992 15:52
58 lines
2538 bytes
In article <1992Aug22.011400.14896@sunb10.cs.uiuc.edu> pjl@sparc6.cs.uiuc.edu (Paul Lucas) writes:

> 	The example I gave was the member-function:

> 		void Link::DeleteAll() {
> 			for ( Link *p = this, *q; p; p = q ) {
> 				q = p->next;
> 				delete p;
> 			}
> 		}

Because this example ultimately says `delete this' (because p == this
and it says `delete p'), I am already tempted to dismiss it out of hand.
I think it's a Bad Idea to write member functions that invalidate their
object, because it is so far away from what lots of people will expect.

Putting that aside, though, I don't see why it's any harder to make DeleteAll
a static member function with an explicit pointer argument:

	/* static */ void Link::DeleteAll(Link* p) {
		for (Link* q; p; p = q) {
			q = p->next;
			delete p;
		}
	}

The code is about the same length; the only difference in use is that
instead of saying

	foo->DeleteAll();

you say

	Link::DeleteAll(foo);

So I don't think this example is a particularly telling argument.

Ditto for the next example (omitted for brevity).

> 	Again, to reiterate (for Andrew's sake), I think it's a
> 	zero-cost feature; code that works now will continue to work,

It is not a zero-cost feature, because it would require compilers to
generate extra code for every call of a member function inherited
from a base class in the presence of virtual inheritance.

> 	and programmers who ignore it will be unaffected, i.e., if they
> 	don't check 'this' their programs will exhibit undefined
> 	behavior (by trying to access members) and "undefined behavior"
> 	is what happens now anyway.

The feature would prohibit C++ implementations from detecting this==0
as an error, which would make at least some genuine mistakes harder to find.


-- 
				--Andrew Koenig
				  ark@europa.att.com
Technical Details
Message-ID: <23528@alice.att.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark
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>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark
From: ark@alice.att.com (Andrew Koenig)
Newsgroups: comp.std.c++
Subject: Re: this == 0 for nonvirtual functions
Keywords: this virtual function undefined
Message-ID: <23528@alice.att.com>
Date: 22 Aug 92 15:52:58 GMT
Article-I.D.: alice.23528
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>
Reply-To: ark@alice.UUCP ()
Distribution: usa
Organization: AT&T Bell Laboratories, Murray Hill NJ
Lines: 58