home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / cplus / 1996 < prev    next >
Encoding:
Text File  |  1993-01-07  |  1.9 KB  |  50 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: C++ already *has* nested functions SO WHAT'S THE BEEF?
  5. Message-ID: <9300811.3521@mulga.cs.mu.OZ.AU>
  6. Keywords: nested functions, dumb ideas
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. References: <1992Dec21.080952.15309@netcom.com> <harvey.726175866@regina>
  10. Date: Fri, 8 Jan 1993 00:39:33 GMT
  11. Lines: 37
  12.  
  13. harvey@opl.com (Harvey Reed) writes:
  14.  
  15. >If programmers want nested functions, it sounds a lot like delegation which
  16. >C++ can handle quite well, as is. Just delegate to another object, or
  17. >your self. This leaves matters fairly explicit.
  18.  
  19. Two points: firstly, I would not agree that C++ (without overloadable
  20. operator dot) handles delegation well; secondly, delegation can be used
  21. as a poor man's substitute for nested functions, but it does *not* have
  22. the same power/expressiveness that nested functions do. The classic
  23. sort of example where nested functions are useful is
  24.  
  25.     somefunc(lots of complex parameters) {
  26.         lots of complex local variables;
  27.  
  28.         int compare(const void *ptr1, const void *ptr2) {
  29.         const object *x = (object *)ptr1;
  30.         const object *y = (object *)ptr2;
  31.         // compare x and y using some complex ordering which is
  32.         // dependant on the above parameters and local variables
  33.         }
  34.  
  35.         ...
  36.         qsort(object_array, n, sizeof(object), compare);
  37.         ...
  38.     }
  39.  
  40. Because the interface to qsort() is fixed, the alternative is to use a rather
  41. ugly hack using global variables (well, ok, you can make them file-scope
  42. static variables rather than real global variables). Here delegation doesn't
  43. help.
  44.  
  45. -- 
  46. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  47. This .signature virus is a self-referential statement that is true - but 
  48. you will only be able to consistently believe it if you copy it to your own
  49. .signature file!
  50.