www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C has nested classes, really?

reply James Dunne <james.jdunne gmail.com> writes:
http://www.digitalmars.com/d/comparison.html

Wow, must've missed something about C a while back! =P
Dec 29 2005
parent reply "Matthew" <matthew hat.stlsoft.dot.org> writes:
And C++ does have perfectly adequate, albeit indirect, support for nested
functions: http://www.cuj.com/documents/s=9949/cuj1135208707797/flex_14.htm

;-)

"James Dunne" <james.jdunne gmail.com> wrote in message
news:dp1fmc$ole$1 digitaldaemon.com...
 http://www.digitalmars.com/d/comparison.html

 Wow, must've missed something about C a while back! =P
Dec 29 2005
parent reply "Walter Bright" <newshound digitalmars.com> writes:
 "James Dunne" <james.jdunne gmail.com> wrote
 Wow, must've missed something about C a while back! =P
"Matthew" <matthew hat.stlsoft.dot.org> wrote in message news:dp1hha$pvh$1 digitaldaemon.com...
 And C++ does have perfectly adequate, albeit indirect, support for nested
 functions: 
 http://www.cuj.com/documents/s=9949/cuj1135208707797/flex_14.htm
I'd disagree with "perfectly adequate." C++ nested functions cannot access local variables in their enclosing functions, necessitating a bunch of extra code to transfer any needed data. In your article, this advantage of D nested functions is not used because you're using a C callback function. If the callback function was a D delegate, the code becomes dramatically simpler: HWND FindFirstChildById(HWND hwndParent, int id) { if(GetDlgCtrlID(hwndParent) == id) { return hwndParent; } else { HWND hwndChild; BOOL FindChildProc(HWND hwnd) { return (GetDlgCtrlID(hwnd) == id) ? (hwndChild = hwnd, false) : true; } EnumChildWindows(hwndParent, &FindChildProc); return hwndChild; } }
Dec 29 2005
parent reply "Matthew" <matthew hat.stlsoft.dot.org> writes:
"Walter Bright" <newshound digitalmars.com> wrote in message
news:dp1jf1$rjn$1 digitaldaemon.com...
 "James Dunne" <james.jdunne gmail.com> wrote
 Wow, must've missed something about C a while back! =P
"Matthew" <matthew hat.stlsoft.dot.org> wrote in message news:dp1hha$pvh$1 digitaldaemon.com...
 And C++ does have perfectly adequate, albeit indirect, support for
nested
 functions:
 http://www.cuj.com/documents/s=9949/cuj1135208707797/flex_14.htm
I'd disagree with "perfectly adequate." C++ nested functions cannot access local variables in their enclosing functions, necessitating a bunch of
extra
 code to transfer any needed data.

 In your article, this advantage of D nested functions is not used because
 you're using a C callback function. If the callback function was a D
 delegate, the code becomes dramatically simpler:

 HWND FindFirstChildById(HWND hwndParent, int id)
 {
   if(GetDlgCtrlID(hwndParent) == id)
   {
     return hwndParent;
   }
   else
   {
       HWND  hwndChild;

     BOOL FindChildProc(HWND hwnd)
     {
       return (GetDlgCtrlID(hwnd) == id)
               ? (hwndChild = hwnd, false)
               : true;
     }

     EnumChildWindows(hwndParent, &FindChildProc);

     return hwndChild;
   }
 }
True. I forgot about that aspect. :$
Dec 29 2005
parent reply "John C" <johnch_atms hotmail.com> writes:
"Matthew" <matthew hat.stlsoft.dot.org> wrote in message 
news:dp1n8l$unj$1 digitaldaemon.com...
 "Walter Bright" <newshound digitalmars.com> wrote in message
 news:dp1jf1$rjn$1 digitaldaemon.com...
 "James Dunne" <james.jdunne gmail.com> wrote
 Wow, must've missed something about C a while back! =P
"Matthew" <matthew hat.stlsoft.dot.org> wrote in message news:dp1hha$pvh$1 digitaldaemon.com...
 And C++ does have perfectly adequate, albeit indirect, support for
nested
 functions:
 http://www.cuj.com/documents/s=9949/cuj1135208707797/flex_14.htm
I'd disagree with "perfectly adequate." C++ nested functions cannot access local variables in their enclosing functions, necessitating a bunch of
extra
 code to transfer any needed data.

 In your article, this advantage of D nested functions is not used because
 you're using a C callback function. If the callback function was a D
 delegate, the code becomes dramatically simpler:

 HWND FindFirstChildById(HWND hwndParent, int id)
 {
   if(GetDlgCtrlID(hwndParent) == id)
   {
     return hwndParent;
   }
   else
   {
       HWND  hwndChild;

     BOOL FindChildProc(HWND hwnd)
     {
       return (GetDlgCtrlID(hwnd) == id)
               ? (hwndChild = hwnd, false)
               : true;
     }

     EnumChildWindows(hwndParent, &FindChildProc);

     return hwndChild;
   }
 }
True. I forgot about that aspect. :$
Er, not quite. EnumChildWindows expects a pointer to a static function, not a delegate.
Dec 29 2005
parent "Walter Bright" <newshound digitalmars.com> writes:
"John C" <johnch_atms hotmail.com> wrote in message 
news:dp1npr$v7k$1 digitaldaemon.com...
 Er, not quite. EnumChildWindows expects a pointer to a static function, 
 not a delegate.
Right, which is why the D version in the article is forced to do it that way. If you're calling a D function, you'd use a delegate.
Dec 29 2005