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

  1. Path: sparky!uunet!opl.com!regina!harvey
  2. From: harvey@opl.com (Harvey Reed)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: C++ already *has* nested functions SO WHAT'S THE BEEF?
  5. Keywords: nested functions, dumb ideas
  6. Message-ID: <harvey.726515064@regina>
  7. Date: 8 Jan 93 17:44:24 GMT
  8. References: <1992Dec21.080952.15309@netcom.com> <harvey.726175866@regina> <9300811.3521@mulga.cs.mu.OZ.AU>
  9. Sender: news@opl.com
  10. Lines: 100
  11.  
  12. fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  13.  
  14. >harvey@opl.com (Harvey Reed) writes:
  15.  
  16. >>If programmers want nested functions, it sounds a lot like delegation which
  17. >>C++ can handle quite well, as is. Just delegate to another object, or
  18. >>your self. This leaves matters fairly explicit.
  19.  
  20. >Two points: firstly, I would not agree that C++ (without overloadable
  21. >operator dot) handles delegation well; secondly, delegation can be used
  22. >as a poor man's substitute for nested functions, but it does *not* have
  23. >the same power/expressiveness that nested functions do. The classic
  24. >sort of example where nested functions are useful is
  25.  
  26. >    somefunc(lots of complex parameters) {
  27. >        lots of complex local variables;
  28.  
  29. >        int compare(const void *ptr1, const void *ptr2) {
  30. >        const object *x = (object *)ptr1;
  31. >        const object *y = (object *)ptr2;
  32. >        // compare x and y using some complex ordering which is
  33. >        // dependant on the above parameters and local variables
  34. >        }
  35.  
  36. >        ...
  37. >        qsort(object_array, n, sizeof(object), compare);
  38. >        ...
  39. >    }
  40.  
  41. >Because the interface to qsort() is fixed, the alternative is to use a rather
  42. >ugly hack using global variables (well, ok, you can make them file-scope
  43. >static variables rather than real global variables). Here delegation doesn't
  44. >help.
  45.  
  46. Good example!
  47.  
  48. The premise for your arguement is that the service call "quicksort"
  49. itself requires a function ("compare"), and that function you want to
  50. define inside "somefunc". In addition, that compare function relies on
  51. parameters scoped to the outer function.
  52.  
  53. Using fixed flat C interfaces is frustrating sometimes. Maybe when 
  54. true object oriented operating systems become commercial we will
  55. be better off :-)
  56.  
  57. In the meantime, explore all the ways that you can package info
  58. for the purposes of interfacing with "flat land". One possiblity
  59. could be:
  60.  
  61. ---------------------
  62.  
  63. class ObjCompare1   // one way to compare an "object"
  64. {
  65.    public:
  66.       static  (lots of complex variables)
  67.  
  68.       static int compare (const void *, const void *);
  69. };
  70.  
  71.  
  72. class ObjCompare2   // another way to compare an "object"
  73. {
  74.    public:
  75.       static  (lots of complex variables)
  76.  
  77.       static int compare (const void *, const void *);
  78. };
  79.  
  80.  
  81. void somefunc(lots of complex parameters)
  82. {
  83.    // now we choose which way to compare the objects,
  84.    // set the (static) data, then use a static function
  85.    // to emulate a flat function.
  86.  
  87.    ObjCompare1::xxx = ... (set all the static data)
  88.  
  89.    qsort(object_array, n, sizeof(object), ObjCompare1::compare);
  90. }
  91.  
  92. ----------------------
  93.  
  94. Advantages: Multiple ways to compare objects, packaged into classes.
  95. We can incrementally add new policies, packaged into classes.
  96.  
  97. Disadvantages: we must make sure the compare choice matches, for
  98. setting data and calling qsort.
  99.  
  100. Obviously my opinion is that the advantages outway the disadvantages.
  101. Until operating systems improve, we will have these kind of "warts".
  102. However, a wart packaged into a class is easier to deal with than
  103. a wart flattened out IMO.
  104.  
  105.  
  106.  
  107. -- 
  108. ++harvey
  109. ========================================================================
  110. internet:  harvey@opl.com  /  hreed@cs.ulowell.edu  /  h.reed@ieee.org
  111. voice:     617-965-0220
  112.