home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19909 < prev    next >
Encoding:
Internet Message Format  |  1993-01-26  |  2.0 KB

  1. Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!news.belwue.de!news.uni-stuttgart.de!news.uni-stuttgart.de!news
  2. From: KOCHER@nvdv.e-technik.uni-stuttgart.dbp.de (HARTMUT KOCHER)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: References to functions?
  5. Date: 26 Jan 1993 08:54:15 GMT
  6. Organization: University of Stuttgart, IND (Germany)
  7. Lines: 46
  8. Distribution: world
  9. Message-ID: <1k2u7nINNgbc@info2.rus.uni-stuttgart.de>
  10. References: <1k0d3dINNev9@info2.rus.uni-stuttgart.de> <1993Jan25.182507.13761@fcom.cc.utah.edu>
  11. NNTP-Posting-Host: nvdv01.ind.e-technik.uni-stuttgart.de
  12. X-News-Reader: VMS NEWS 1.24
  13. In-Reply-To: swillden@news.ccutah.edu's message of Mon, 25 Jan 93 18:25:07 GMT
  14.  
  15. In <1993Jan25.182507.13761@fcom.cc.utah.edu> swillden@news.ccutah.edu writes:
  16. > : > References to functions are legal, but not very useful.  I can't
  17. > : > think of anything you can do with a reference to a function you
  18. > : > can't do with a const pointer to a function in exactly the same way,
  19. > : > except for trivial syntax differences at the point of initialization.
  20. > : > Steve Clamage, TauMetric Corp, steve@taumet.com
  21. > : Not exactly. Let's say I have a callback class which takes a pointer
  22. > : to a function in its constructor. If I use a pointer, a 0-pointer may be
  23. > : passed. If I use a reference instead, I can make sure, that the user
  24. > : supplies a valid function.
  25. > Not really.  Try:
  26. > typedef int (callbackfunc)(int);
  27. > extern void set_callback(callbackfunc&);
  28. > void foo()
  29. >     {
  30. >     callbackfunc* callback = 0;
  31. >     set_callback(*callback);
  32. >     }
  33. > : Hartmut Kocher
  34. > Shawn Willden
  35. > swillden@icarus.weber.edu
  36.  
  37. Sure, but it's always undefined to dereference a 0 pointer.
  38. With a reference you always clearly state, that you are 
  39. expecting a valid object/function, etc.
  40.  
  41. Therefore, it's the callers responsibility to make sure he's passing a valid
  42. argument, whereas with pointers, the function implementor has to check for
  43. 0 pointers, because these are allowed by the language.
  44.  
  45. Hartmut Kocher
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.