Function pointers

Started by
7 comments, last by M2tM 18 years, 3 months ago
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.
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);
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 pointervoid (MyClass::*functionPtr)(int);

This is declaring a pointer to a member function. Ok.
// Assign an exsiting function to the function pointerfunctionPtr = &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);


Stephen M. Webb
Professional Free Software Developer

What if I want to call the function pointer from within the class? Would I just use the this pointer?
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.
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).
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.
Consider looking into functors/function objects, specifically boost::function and boost::bind.
I'll agree, that is a good case for function pointers.

*edit: read the post above though.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

This topic is closed to new replies.

Advertisement