Archived

This topic is now archived and is closed to further replies.

Themis

classes & function pointers

Recommended Posts

Hello, I want to ask about pointers to functions in C++. When I want to use a function pointer in C I use a simple typedef like the following: typedef int (*pFunc)(void *); and then I give as an argument the name of a proper function in another one. My problem is that if the function whose pointer I want to give is an object''s method then the compiler starts complaining. eg. void foo(int x, pFunc func); class cClass { int myFunc(void *x); }; int main() { cClass myClass; int x=0; foo(x, myClass.myFunc); //compiler complaints return 0; } -------------------------------------------------------------------------------- How can I overcome this problem? I ''ve tried to typecast the method eg foo(x, (pFunc)myClass.myFunc); but this as you might know doesn''t work . Can you help me with this? Thanks, Themis

Share this post


Link to post
Share on other sites
your best source for information on this is http://www.function-pointer.org/
to make it short:


  
typedef void ( myclass::*FUNC ) ( int );
myclass obj; // instantiate myclass

FUNC f = obj.myfunc; // assign adress

( obj.*f ) ( 123 ); // and call it





Runicsoft -- latest attraction: obfuscated Brainfuck Interpreter in SML

This post was made entirely from re-cycled electrons

Share this post


Link to post
Share on other sites