Jump to content
  • Advertisement
Sign in to follow this  
Mantear

Function pointers

This topic is 4612 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

I read the article by Mickela found on this site concerning function pointers and how to use them with classes, but I'm still having trouble using them. I've been able to declare the function pointer variable, assign a function to it, but I get a compiler error when I try to call it. Here's my code setup:// Create the function pointer void (MyClass::*functionPtr)(int); // Assign an exsiting function to the function pointer functionPtr = &MyClass::MyFunction; // Call the function (*functionPtr)(50);[\code] According to the article, the second line should be functionPtr = MyFunction. However, that doesn't compile for me. The compiler suggested to use the above example, adding the &MyClass::, which does compile. The final line does not compile, throwing an error that "'*' is an illegal operand to use on 'void (__thiscall MyClass::* )(int)'" and that it does not resolve to a function taking 1 argument. Any ideas? Thanks. EDIT: I'm using C++ with VS 2005 Express.

Share this post


Link to post
Share on other sites
Advertisement
In order to use a pointer to member function you need to bind it to an object instance.

MyClass a;
MyClass b = new MyClass;

(a.*functionPtr)(50);
(b->*functionPtr)(5);

Share this post


Link to post
Share on other sites
Quote:
Original post by Mantear
I read the article by Mickela found on this site concerning function pointers and how to use them with classes, but I'm still having trouble using them. I've been able to declare the function pointer variable, assign a function to it, but I get a compiler error when I try to call it. Here's my code setup:


// Create the function pointer
void (MyClass::*functionPtr)(int);

This is declaring a pointer to a member function. Ok.

// Assign an exsiting function to the function pointer
functionPtr = &MyClass::MyFunction;

This is assigning a pointer to a member function to yur variable. All good (but it can be done on a single line so you don't have an uninitialized variable).

// Call the function
(*functionPtr)(50);[\code]

Oops, that calling a function. You need to bind the pmf to an object and call the member function.

MyClass *object = getSomeObjectPointer();
(object->*functionPtr)(50);


Share this post


Link to post
Share on other sites
If you want the member function pointer to be called on the object referred to by this, then you can bind the function pointer to the this pointer.

Share this post


Link to post
Share on other sites
If you're posting in For Beginners and you want to do this, I'm afraid I'm going to have to ask you why you think you want to do this. I strongly suspect there is a cleaner approach to whatever it is you're doing (on a broader scale).

Share this post


Link to post
Share on other sites
I'm practicing at putting together a GUI. I put together a system to easily generate the Menus. One part of this involves linking a function to a button. I didn't want to end up with massive switch/case statement to parse through which function should be called when a button is pressed.

Share this post


Link to post
Share on other sites
I'll agree, that is a good case for function pointers.

*edit: read the post above though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!