Jump to content
  • Advertisement

Archived

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

Themis

classes & function pointers

This topic is 5722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!