C++ Question: Function pointers to member functions
Posted: 12 Mar 2008, 07:12
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:
So, basically, can I create a pointer to a method in this way, and how would I go about doing it?
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:
...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).Visual Studio is stupid wrote:error C3867: 'CSimpleObject::Update': function call missing argument list; use '&CSimpleObject::Update' to create a pointer to member
So, basically, can I create a pointer to a method in this way, and how would I go about doing it?