Function pointer point to a object member function in C++?
-
Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,
-
Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,
Here is the long version: Member Function Pointers and the Fastest Possible C++ Delegates But this site may be more helpful to start with: http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]
-
Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,