Skip to main content
gamedev.net
🔒 Locked

function pointer passed from within a class

Started by Funkymunky Jun 28, 2008 at 1:01 PM 9 replies 1.2k views
Original Post
Funkymunky
Funkymunky
I'm a little new to function pointers (C++), and this part is confusing me. I have a global function for Keypress callbacks, like so:

void KeyPressCallback(int k, void (*p)());
I have a class which wants to use this function. Except I can't figure out how to pass one of its member functions ("void AKey();") as the parameter. I've tried these from within the class's initialization function:

KeyPressCallback(keyA, this->AKey);
KeyPressCallback(keyA, &ManagerClass::AKey);
both spur errors. I also tried this from the function which calls the class's initialization function, after that call:

KeyPressCallback(keyA, gManager.AKey);
and still no go. So, what would be the correct syntax/method for doing what I'm trying to do?
Gage64
Gage64
You are getting errors because the function expects a pointer to a free (i.e., non-member) function, and you're trying to pass it a pointer to a member function.

If you want the function to accept a pointer to a member function, the signature should look like this:

void KeyPressCallback(int k, void (MyClass::*p)());

BTW, of the 3 function calls you listed, the second one is the correct one.

EDIT: You also need to pass a pointer to the object that the function is to be invoked on. See here and here for more info on pointers to members.
fpsgamer
fpsgamer
It is not enough to just supply a member function pointer. You must also supply an instance to invoke the function on.
nullsquared
nullsquared
Except now you'll need an instance of the class to call the member function pointer on:
someInstance->(*memberFunctionPointer)(parameters);

Otherwise you'll want to use static functions.
Funkymunky
Funkymunky
thorough and near-instantaneous replies, thank you
Funkymunky
Funkymunky
hmmm... can I do it with a class member function that is inherited from a pure virtual class? like this:

class Manager{public:    virtual void AKey() = 0;};class WorldManager : public Manager{public:    void Init();    void AKey() { // do something }};void KeyPressCallback(int k, Manager *M, void (Manager::*p)());void WorldManager::Init(){    KeyPressCallback(keyA, this, &WorldManager::AKey);}


This gives me an error that it can't covert the function pointer namespace from WorldManager to Manager... is there a way to?

[Edited by - Funkymunky on June 28, 2008 3:02:58 PM]
fpsgamer
fpsgamer
Quote:
Original post by Funkymunky
This gives me an error that it can't covert the function pointer namespace from WorldManager to Manager... is there a way to?


It might have something to do with the fact that you forgot to have WorldManager inherit from Manager.

class WorldManager : public Manager{public:    void Init();    void AKey() { // do something }};


But why use a function pointer and a free function in the first place? Why not just utilize polymorphsim ?

void handleKeyEvent(Manager& man){     man->AKey(); // All managers define this function}


Supplying an instance and a function pointer is redundant. You're trying to apply C style callback idioms to C++ when they aren't really necessary.
Antheus
Antheus
Quote:
Original post by Funkymunky
hmmm... can I do it with a class member function that is inherited from a pure virtual class? like this:

This gives me an error that it can't covert the function pointer namespace from WorldManager to Manager... is there a way to?


So you have control over callback design?

Why use such way in the first place then? This approach is usually used for legacy C API, which relied on function pointers.

Under C++, you have choice of polymorphism or boost::function (and equivalent solutions).
Funkymunky
Funkymunky
oops, the code is just a simple representation of what I'm attempting, I edited it such that WorldManager inherits from Manager (which I have in my full code already)

I'm essentially attempting to have a generic callback method for keypresses, such that any class which inherits from Manager can specify a member function of its own that should be called when a key is pressed.
fpsgamer
fpsgamer
Quote:
Original post by Funkymunky
oops, the code is just a simple representation of what I'm attempting, I edited it such that WorldManager inherits from Manager (which I have in my full code already)

I'm essentially attempting to have a generic callback method for keypresses, such that any class which inherits from Manager can specify a member function of its own that should be called when a key is pressed.


boost::function and boost::bind
Funkymunky
Funkymunky
then is it even possible to do it with an inherited virtual class member function?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.