home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / std / cplus / 1098 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.0 KB  |  39 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: this == 0 for nonvirtual functions
  5. Message-ID: <1992Aug20.215455.17279@microsoft.com>
  6. Date: 20 Aug 92 21:54:55 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Aug18.045605.14220@sunb10.cs.uiuc.edu>
  9. Keywords: this virtual function undefined
  10. Lines: 27
  11.  
  12. In article <1992Aug18.045605.14220@sunb10.cs.uiuc.edu> pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
  13. |    To reiterate, all I would like is to guarantee that the *call*
  14. |    to a non-virtual member-function will succeed and that it's my
  15. |    responsibility to check for a non-nil 'this'.
  16.  
  17. First, note that this would not come at "zero" cost.  As in the case of
  18. assigning to a reference, requiring "this" to be non-null allows compilers
  19. to avoid generating code to preserve nullness when adjusting pointers
  20. in the case of MI.  In the case of references, this issue has already
  21. been reviewed and decided upon: one is not allowed to have null references.
  22.  
  23. Further, note that member functions can be called either on a pointer or
  24. on a reference.  If the language were to PERMIT calling member functions
  25. on a null pointer, such would not ALLOW *you* to test for null "this" inside
  26. you member functions, but rather would REQUIRE all of *us* to test for
  27. null "this" inside ALL *our* non-virtual member functions -- because a user
  28. of our class such as yourself could then legally call *our* member functions via
  29. a null pointer.  If such were the case then, the only sane remedy C++
  30. programmers would have would be to declare all their member functions virtual.
  31. Then *you* *still* could not legally call these members on a null pointer --
  32. but many member functions would now be needlessly slow and generate needlessly
  33. large code.
  34.  
  35. I suggess instead that you use friend functions or static member functions 
  36. where you feel you must address null pointers implicitly.  Alternately, note
  37. that the users of your class may be the ones in the best position to perform
  38. the null pointer tests [explicitly].
  39.