Page 1 of 1

C++ Question: Function pointers to member functions

Posted: 12 Mar 2008, 07:12
by Felix the Cat
Hey, I was wondering if any of the leet codewarriors here could help me with an issue.

I have a class CGame with a method RegisterSimulatedObject which takes a pointer to a SimulatedObject (ABC) and five function pointers. The intention is for CGame to call the functions that it is given at various points (Render, Update physics, etc) in order to keep all behaviors at a class level and so be more amenable to plugging in new classes with new behaviors.

Clearly a better solution to this, one that I'll do as soon as I finish with this post, is to drop the function pointers and call each of the functions from the SimulatedObject pointer given. Only minor issue is that things that don't have, say, physics code (i.e. walls) will still have to have their empty Update and HandleCollision functions called rather than passing in NULL for these functions - oh well - maybe Visual Studio optimizes calls to empty functions at compile time anyways.

But I still want to know what the issue is. As a test, I'm creating one object of type SimpleObject (derived from SimulatedObject) on the heap and then passing its pointer and its 5 function pointers to RegisterSimulatedObject. However, Visual Studio doesn't like this. It gives me the least helpful error ever:
Visual Studio is stupid wrote:error C3867: 'CSimpleObject::Update': function call missing argument list; use '&CSimpleObject::Update' to create a pointer to member
...which is clearly unhelpful as "&CSimpleObject::Update" doesn't exist (the function is not static, and VS doesn't like it when I make the functions static).

So, basically, can I create a pointer to a method in this way, and how would I go about doing it?

Re: C++ Question: Function pointers to member functions

Posted: 12 Mar 2008, 13:07
by imbaczek
I didn't quite understand what you exactly want, but I know that C++ member method pointers are scary stuff. I'd advise to stay away from them unless you know that they'll 100% solve your problem.

Re: C++ Question: Function pointers to member functions

Posted: 12 Mar 2008, 13:16
by Kloot
What you really want are delegates, but in C++ you only have
(non-static) member function pointers. They are an _extremely_
murky area of the language specification though, due to issues
arising from the freedom allowed by multiple inheritance. To get
a pointer to a member function that eats a float and returns an
int, you have to say:

Code: Select all

int (MyClass::*MyFuncP)(float) = &MyClass::MyFunc;
And to dereference it:

Code: Select all

MyClass* mcP = new MyClass();
(mcP->*MyFuncP)(123.456f);
or

Code: Select all

MyClass mc;
(mc.*MyFuncP)(456.789f);

Re: C++ Question: Function pointers to member functions

Posted: 12 Mar 2008, 19:00
by imbaczek
if delegates are what you want, use boost::signals.

Re: C++ Question: Function pointers to member functions

Posted: 13 Mar 2008, 00:07
by Felix the Cat
Thanks for the help... looks like essentially, if you think you need member function pointers, you're probably going about it in the wrong way, eh?

As I said originally it only took me several hours to figure out that if I'm passing a pointer to an object I don't need to pass pointers to its public methods :oops:.