home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!opl.com!regina!harvey
- From: harvey@opl.com (Harvey Reed)
- Newsgroups: comp.std.c++
- Subject: Re: C++ already *has* nested functions SO WHAT'S THE BEEF?
- Keywords: nested functions, dumb ideas
- Message-ID: <harvey.726515064@regina>
- Date: 8 Jan 93 17:44:24 GMT
- References: <1992Dec21.080952.15309@netcom.com> <harvey.726175866@regina> <9300811.3521@mulga.cs.mu.OZ.AU>
- Sender: news@opl.com
- Lines: 100
-
- fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
-
- >harvey@opl.com (Harvey Reed) writes:
-
- >>If programmers want nested functions, it sounds a lot like delegation which
- >>C++ can handle quite well, as is. Just delegate to another object, or
- >>your self. This leaves matters fairly explicit.
-
- >Two points: firstly, I would not agree that C++ (without overloadable
- >operator dot) handles delegation well; secondly, delegation can be used
- >as a poor man's substitute for nested functions, but it does *not* have
- >the same power/expressiveness that nested functions do. The classic
- >sort of example where nested functions are useful is
-
- > somefunc(lots of complex parameters) {
- > lots of complex local variables;
-
- > int compare(const void *ptr1, const void *ptr2) {
- > const object *x = (object *)ptr1;
- > const object *y = (object *)ptr2;
- > // compare x and y using some complex ordering which is
- > // dependant on the above parameters and local variables
- > }
-
- > ...
- > qsort(object_array, n, sizeof(object), compare);
- > ...
- > }
-
- >Because the interface to qsort() is fixed, the alternative is to use a rather
- >ugly hack using global variables (well, ok, you can make them file-scope
- >static variables rather than real global variables). Here delegation doesn't
- >help.
-
- Good example!
-
- The premise for your arguement is that the service call "quicksort"
- itself requires a function ("compare"), and that function you want to
- define inside "somefunc". In addition, that compare function relies on
- parameters scoped to the outer function.
-
- Using fixed flat C interfaces is frustrating sometimes. Maybe when
- true object oriented operating systems become commercial we will
- be better off :-)
-
- In the meantime, explore all the ways that you can package info
- for the purposes of interfacing with "flat land". One possiblity
- could be:
-
- ---------------------
-
- class ObjCompare1 // one way to compare an "object"
- {
- public:
- static (lots of complex variables)
-
- static int compare (const void *, const void *);
- };
-
-
- class ObjCompare2 // another way to compare an "object"
- {
- public:
- static (lots of complex variables)
-
- static int compare (const void *, const void *);
- };
-
-
- void somefunc(lots of complex parameters)
- {
- // now we choose which way to compare the objects,
- // set the (static) data, then use a static function
- // to emulate a flat function.
-
- ObjCompare1::xxx = ... (set all the static data)
-
- qsort(object_array, n, sizeof(object), ObjCompare1::compare);
- }
-
- ----------------------
-
- Advantages: Multiple ways to compare objects, packaged into classes.
- We can incrementally add new policies, packaged into classes.
-
- Disadvantages: we must make sure the compare choice matches, for
- setting data and calling qsort.
-
- Obviously my opinion is that the advantages outway the disadvantages.
- Until operating systems improve, we will have these kind of "warts".
- However, a wart packaged into a class is easier to deal with than
- a wart flattened out IMO.
-
-
-
- --
- ++harvey
- ========================================================================
- internet: harvey@opl.com / hreed@cs.ulowell.edu / h.reed@ieee.org
- voice: 617-965-0220
-