2011
03.01

I read some interesting articles about nested function by C[1][2] and thought this is easy to be implemented by C++, so I wrote one C++ version.

1. 在 C 语言中包装函数 — Closure 和 GCC nested function
2. GCC 的 nested function 與 trampoline


#include 〈iostream〉

using namespace std;

typedef int (*func_t)(int arg);

class create_wrap_function
{
public:
create_wrap_function( func_t f):wrapped_func(f){}
int operator()( int arg)
{
cout << "yooo" << endl;
wrapped_func( arg);
}
private:
create_wrap_function();
func_t wrapped_func;
};

int foo(int a)
{
return a + 1;
}

int main( void)
{
create_wrap_function bar( foo);
cout << bar(2) << endl;
return 0;
}

No Comment.

Add Your Comment

Spam Protection by WP-SpamFree