# Include <iostream> Class Object {}; typedef int (Object: * sayfunc) (const char *); Class A: Public object {public: int say (const char * MSG) {STD: cout <"A say" <MSG <STD: Endl; return 0 ;}; Class B: public object {public: int run (const char * MSG) {STD: cout <"B run" <name <MSG <STD: Endl; return 0;} Char name [64] ;}; class controller {public: controller (sayfunc fun, object * Target) {This-> fun = fun; This-> Target = target ;}~ Controller () {} void dofun (const char * MSG) {// (Target-> * Fun) (MSG );} PRIVATE: sayfunc fun; object * target;}; int main (void) {A * pA = new A; Controller * pcontroller1 = new controller (sayfunc) & A: Say, pa); pcontroller1-> dofun ("XXX1"); Controller * pcontroller2 = new controller (sayfunc) & B: Run, PA/* even if the passed PA pointer can call the member function of B, it is similar to forcibly converting a * to B **/); pcontroller2-> dofun ("xxx2 "); B * pb = new B; strcpy (Pb-> name, "Vicky"); Controller * pcontroller3 = new controller (sayfunc) & B: Run, Pb ); pcontroller3-> dofun ("xxx3"); System ("pause"); Return 0 ;}
A say XXX1
B run xxx2
B run vickyxxx3
Press any key to continue...