Nesting Functions in C++
Index
If you are a C++ developer who has never learned or used Pascal, the usefulness of this tip may be lost to you.
Hopefully you won't neglect the benefits that this tip can bring you, namely better organized code and a resulting
ease of maintenance.
In Pascal, you can take portions of a Function or Procedure and break them out, like so:
function sample( parm1: Cardinal, parm2: Cardinal ): Cardinal;
var
_local_1: Cardinal;
function nested( parm1: Cardinal , parm2: Cardinal ): Cardinal;
begin
if ( parm1 = 0 ) Then
Result := parm2;
else
Result:= nested( parm1 - 1 , parm2 * 2 );
end;
begin
Result := 0;
for _local_1 := 0 to parm2
do
Result := Result + nested( _local_1 , parm1 );
end;
end;
However, C++ has no direct means of creating nested functions. ...unless you take advantage of one of the
more subtle aspects of C++. In C++, you can create local classes, and like all classes, local classes
can have static member functions, which means of course, that you can call these member functions. To illustrate,
here is the previous example translated to C++.
unsigned int sample( unsigned int parm1 , unsigned int parm2 )
{
class _local
{
public: static unsigned int nested( unsigned int parm1 , unsigned int parm2 )
{
if ( parm1 == 0 )
return parm2;
else
return nested( parm1 - 1 , parm2 * 2 );
}
};
unsigned int _result = 0;
for( unsigned int _local_1 = 0 ; _local_1 <= parm2 ; ++_local_1 )
_result += _local::nested( _local_1 , parm1 );
return _result;
}
If you notice, there is a call to _local::nested( ) after the definition of the local class _local. This looks an
awful lot like the Pascal call to nested, doesn't it? It also organizes the source code for sample. Of course,
you would use this technique more for routines with more complex flow.
Nested Classes FAQs
- Q: Shouldn't I just make my nested functions "un-nested" so I can reuse them later?
- A: When it makes sense to have your functions reusable, then by all means doing so is a better
approach. I have found a number of cases where doing so only clutters the namespace.
- Q: Speaking of namespaces, why not just put these functions into a namespace, like sample_helpers?
- A: Well, functions in that namespace would be accessible to all. In some cases, you don't want that.
- Q: Why name your local class "_local"? Do I have to do that?
- A: As with any naming convention, this is just a convention. Call your local classes whatever you want. I
prefer "_local" because it reinforces the fact that I'm calling a local function and not something accessible
to other functions.
- Q: Does this work in C++ class member functions?
- A: You betcha.
- Q: What a neat idea! How'd you come up with this?
- A: Back in school I used Delphi quite a bit, and took advantage of its ability to nest functions. When I
switched to C and C++, I truly missed the ability to build nested functions. When I discovered that you could
create a class in the middle of a function, I instantaneously realized that this technique was possible and
have been using it ever since.