Article View: comp.std.c++
Article #183Re: this == 0 for nonvirtual functions
From: Anders Juul Munch
Date: Tue, 25 Aug 1992 17:28
Date: Tue, 25 Aug 1992 17:28
29 lines
1460 bytes
1460 bytes
pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
> void Link::DeleteAll() {
> for ( Link *p = this, *q; p; p = q ) {
> q = p->next;
> delete p;
> }
> }
You are deleting the first list member using its own DeleteAll method and
the other members using the first member's DeleteAll method.
To see what the problems can arise from this, make DeleteAll virtual and
redefine it in a subclass:
class MyLink : Link {
void DeleteAll() { ExtraBookkeeping(); Link::DeleteAll(); }
};
Now MyLink's will be DeleteAll'ed with the wrong method if a plain Link
heads the list. And to make things worse, such a bug would be almost
impossible to find.
However,
static Link::DeleteAll(Link* head);
does not have this problem and also note that `head' can legally be NULL,
solving your original problem.
Anders Munch | Department of Computer Science
juul@diku.dk | University of Copenhagen, Denmark
"Confused? The thing is to get confused at a higher level" - Jorgen Thorslund
Technical Details
Message-ID:
<1992Aug25.172850.22543@odin.diku.dk>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!dkuug!diku!juulReferences:
<1992Aug18.045605.14220@sunb10.cs.uiuc.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!dkuug!diku!juul From: juul@diku.dk (Anders Juul Munch) Newsgroups: comp.std.c++ Subject: Re: this == 0 for nonvirtual functions Keywords: this virtual function undefined Message-ID: <1992Aug25.172850.22543@odin.diku.dk> Date: 25 Aug 92 17:28:50 GMT References: <1992Aug18.045605.14220@sunb10.cs.uiuc.edu> Sender: juul@rimfaxe.diku.dk Organization: Department of Computer Science, U of Copenhagen Lines: 29