2011-04-01から1ヶ月間の記事一覧

クラスのインスタンスとメンバ関数を引数に持つコールバック関数

#include <stdio.h> class myclass { public: myclass() { value = 5; } void foo() { printf("myclass::foo i = %d\n", value); } int value; }; void callback(myclass *p, void (myclass::*function)()) { (p->*function)(); } int main() { puts("main"); myclass</stdio.h>…

C++のメンバ関数をメモリに保存し、呼び出す方法

クラスを使用せずにC言語のみであれば、下記コードのように関数のポインタを保存し呼び出すことが可能です。 =====test1.cpp===== #include void (*func)() = 0;void foo() { puts("foo"); }int main() { puts("main"); func = foo; func(); } =============…