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