Friday, March 7, 2008

Tricking the C++ compiler to use inner functions

Inner functions are great - they allow you to use organize code and encapsulate it in the outer functions scope. Most languages support inner functions and there aren't any good reasons why C++ wouldn't support this feature.

Well it does actually here's how...

Wrap the function inside a local class (!?). Yes, you can define a local class/struct inside a function, and then use the local class's functions willy-nilly.

It's verbose but it works well, here's a snippit:


void Foo() {

struct BarFunction {
static void Bar() { /*...*/ }
};

BarFunction::Bar();
}

~Z~

No comments: