// ( t9.C ) // 関数へのポインタ:クラスで使う場合 // #include void sub1( void ) { cout << "sub1 desu\n" ; } void sub2( char* str ) { cout << "sub2 desu : " << str << endl ; } // 例題1 class Draw { int normal,rubber,*work ; void (*FUNC)(void) ; public: Draw( void (*PF)(void) ) ; // 関数を渡す、ただの Draw(void) はダメ void set_normal() { work = &normal ; } void set_rubber() { work = &rubber ; } void type() { cout << *work << endl ; } void do_FUNC( void ) { (*FUNC)() ; } // FUNC() でもよい void do_function( void (*PF)(void) ) { (*PF)() ; } void do_function( void (*PF)(char*),char* s ) { (*PF)(s) ; } } ; // 任意の引数を渡すプログラムは難しい Draw::Draw( void (*PF)(void) ) { FUNC = PF ; // 注目! normal = 0 ; rubber = 1 ; set_normal() ;// おまけの部分 } // 例題2 class PPP { int x ; public: PPP( int p ) { x = p ; }; void print(void) { cout << "PPP = " << x << endl ; } } ; typedef void ( PPP::*CF1)() ; CF1 cf ; // [方法その1] void ( PPP::*CF2)(void) ; // [方法その2] void main() { // 例題1 Draw d( &sub1 ) ; d.do_FUNC() ; // sub1 desu d.do_function( &sub1 ) ; // sub1 desu d.do_function( &sub2,"Katou" ) ; // sub2 desu : Katou // 例題2 PPP p(10) ; cf = &PPP::print ; (p.*cf)() ; // PPP = 10 CF2 = &PPP::print ; (p.*CF2)() ; // PPP = 10 d.set_rubber(); d.type() ; // 1 これはおまけ }