Jump to content
  • Advertisement
Sign in to follow this  
Funkymunky

function pointer passed from within a class

This topic is 3764 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'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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
It is not enough to just supply a member function pointer. You must also supply an instance to invoke the function on.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

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!