Re: nested functions in VC++ by Simon
Simon
Sun Apr 24 18:06:21 CDT 2005
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message >
> Why do you think you want nested functions?
> Typically, the only things that nested functions add are:
>
> 1. more limited scope of name visibility.
> 2. access to "up level" local variables (if c() is in b() which is in a(),
> c() can access b()'s local variables as well as a()'s).
>
> Use of classes instead of free functions more or less supplies 1. 2 can
> be accomplished manually by passing references to the required up-level
> variables.
Indeed. The common case where I want nested functions is in recursive
functions, to supply the outer case. Typically, a recursive function may act
on some state, and either we pass it down the stack every time or create an
outer function which it can access. Generally now I use an anonymous
namespace to deal with this, but this still is polluting the namespace, be
it only by one name.
I *think* I have a reasonable argument that nested functions are just
anonymous classes (in the Java sense) that have names, but I'm not entirely
sure.
S.
"Whereof one cannot speak, thereof one must be silent." -- Wittgenstein.
"If you don't know, shut up." -- The wife.