関数内で関数を定義する
つまりはクロージャが欲しい.
C言語の場合は GCC 拡張でこれを実現できる.
http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
#include <stdio.h> int main(void) { int x = 0; void f(void) { x++; } f(); printf("%d\n", x); return 0; }
C++ の場合は C++0x の lambda で実現できた.
http://cpplover.blogspot.com/2008/03/c0xlambda.html
#include <iostream> #include <functional> int main() { int x = 0; const std::function<void ()> f = [&]() { x++; }; f(); std::cout << x << std::endl; }