home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / std / cplus / 1117 < prev    next >
Encoding:
Internet Message Format  |  1992-08-27  |  1.4 KB

  1. Path: sparky!uunet!mcsun!sunic!dkuug!diku!juul
  2. From: juul@diku.dk (Anders Juul Munch)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: this == 0 for nonvirtual functions
  5. Keywords: this virtual function undefined
  6. Message-ID: <1992Aug25.172850.22543@odin.diku.dk>
  7. Date: 25 Aug 92 17:28:50 GMT
  8. References: <1992Aug18.045605.14220@sunb10.cs.uiuc.edu>
  9. Sender: juul@rimfaxe.diku.dk
  10. Organization: Department of Computer Science, U of Copenhagen
  11. Lines: 29
  12.  
  13. pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
  14.  
  15. >        void Link::DeleteAll() {
  16. >            for ( Link *p = this, *q; p; p = q ) {
  17. >                q = p->next;
  18. >                delete p;
  19. >            }
  20. >        }
  21.  
  22. You are deleting the first list member using its own DeleteAll method and
  23. the other members using the first member's DeleteAll method.
  24. To see what the problems can arise from this, make DeleteAll virtual and
  25. redefine it in a subclass:
  26.     class MyLink : Link {
  27.       void DeleteAll() { ExtraBookkeeping(); Link::DeleteAll(); }
  28.     };
  29.  
  30. Now MyLink's will be DeleteAll'ed with the wrong method if a plain Link
  31. heads the list. And to make things worse, such a bug would be almost
  32. impossible to find.
  33.  
  34. However, 
  35.     static Link::DeleteAll(Link* head);
  36. does not have this problem and also note that  `head' can legally be NULL,
  37. solving your original problem.
  38.  
  39. Anders Munch        |  Department of Computer Science
  40. juul@diku.dk        |  University of Copenhagen, Denmark
  41. "Confused? The thing is to get confused at a higher level" - Jorgen Thorslund
  42.