home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18199 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  2.9 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!agate!agate!matt
  2. From: matt@physics.berkeley.edu (Matt Austern)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Give me safe C++
  5. Date: 18 Dec 92 10:07:13
  6. Organization: Lawrence Berkeley Laboratory (Theoretical Physics Group)
  7. Lines: 40
  8. Message-ID: <MATT.92Dec18100713@physics.berkeley.edu>
  9. References: <Bz2nDF.7B6@fiu.edu> <1992Dec12.145403.26483@ucc.su.OZ.AU>
  10.     <1992Dec14.190553.14838@mole-end.matawan.nj.us>
  11.     <1992Dec17.192301.23525@ucc.su.OZ.AU>
  12.     <HF.92Dec18115942@whyaduck.tk.telematik.informatik.uni-karlsruhe.de>
  13. Reply-To: matt@physics.berkeley.edu
  14. NNTP-Posting-Host: physics.berkeley.edu
  15. In-reply-to: hf@tk.telematik.informatik.uni-karlsruhe.de's message of 18 Dec 92 11:59:42
  16.  
  17. In article <HF.92Dec18115942@whyaduck.tk.telematik.informatik.uni-karlsruhe.de> hf@tk.telematik.informatik.uni-karlsruhe.de (Harald Fuchs) writes:
  18.  
  19. > I agree that they are sometimes quite handy, but I've never missed
  20. > them. In the old days when I was a Modula-2 addict, I used them most
  21. > of the time in a situation where I needed a function call in a
  22. > specific context (which in turn can be expressed as a kind of
  23. > "state"), and this is exactly what objects are for.
  24.  
  25. This is indeed exactly what objects are for; there is, however, one
  26. small problem.
  27.  
  28. Suppose that you need to pass a function in a specific context to
  29. another routine *as a function pointer*.  Objects, in this case,
  30. aren't useful: you can't pass a pointer to an object (or, for that
  31. matter, a pointer to a member function) when the routine you're using
  32. expects a pointer to a function.
  33.  
  34. Possible solutions:
  35.     (1) Rewrite the routine so that, instead of expecting a pointer 
  36.         to a function, it expects a Function, where Function is an
  37.         abstract function-like class.  (You can even provide 
  38.         conversion operators so that your routine will do the right
  39.         thing if the user just gives it a function pointer.)
  40.     (2) Use a static function, and encode the state using static 
  41.         variables.  This is a clumsy solution (it puts state variables
  42.         in file scope instead of class scope, and it will do horrible
  43.         things if this function ever calls itself recursively), but,
  44.         for simple enough cases, it will work.
  45.  
  46. The first of these methods is clearly the right one, and I think that
  47. it does provide most of the functionality of nested functions.  Still,
  48. I have to admit that I think nested functions would be nice.  I miss
  49. them most when I'm doing a two-dimensional integration; what I want to
  50. do would be trivial in Scheme, but is somewhat clumsier in most of the
  51. other languages (including C++) that I've used.
  52. --
  53. Matthew Austern                   Just keep yelling until you attract a
  54. (510) 644-2618                    crowd, then a constituency, a movement, a
  55. austern@lbl.bitnet                faction, an army!  If you don't have any
  56. matt@physics.berkeley.edu         solutions, become a part of the problem!
  57.