Jump to content

Function pointer with a class function


ParaToxic
 Share

Recommended Posts

Hei ya,

I wan't to add a function SetButtonFunction to my MenuSystem to call a given function when the button is pressed.

 

In my button class I have a void pointer -> void (*_func)(void);

 

I just have to call SetButtonFunction(IMenuButton* b, void (*_func)(void)); and add the function.

But when I enter a class owned function it doesn't work.For example I have in my class the function Test() --> (IMenuSystem::Test()) and enter that to the function it doesn't work.I become a error....something with a argumentlist.... but when I just create a custom function in the source file void Test() { MessageBoxA(0,"Hey",0,0); } it works fine.

 

Have someone a idea how to manage that ?

Link to comment
Share on other sites

This seems to be exactly what you are looking for.

 

Instead of storing the function pointer in your button class Just store an Event0 object. Then bind your function in your other class to that. In order to call class methods you have to store a pointer to that class as well as the function pointer. This is what the Event classes I linked to is doing.

 

Here is an example on how to do it but you'll notice it's very specific to the 1 class. I assume you want this to be generic enough to handle member functions from any class, and that's what the code I linked to above is doing.

http://publib.boulde...ref/cplr034.htm

 

 

An "good" event system is one that allows calling member functions to other classes. Seems to be the same thing you are asking for here. They are one in the same.

 

 

Function pointers to class methods has to be treated differently than normal stand alone. You must have a pointer to the class in order to make the call. Again, I think you'll find what I linked to does what you want very easily and is very flexible to work between any classes and wraps it all up in a nice little class to avoid dealing with function pointers.

Link to comment
Share on other sites

Don't know if I understood the question correctly but this might be what you are asking for

 

typedef void (*TheFunc)(void);

class Adam
{
   TheFunc _func;

public:
   Adam() : _func(0) {}

   void SetFunc( TheFunc func  ) { _func = func; }

   void Call()
   {
         if( _func )
              _func();
   }
};

void MyFunc()
{
     // Called ....
}


int main( int, char*[])
{
     Adam a;
     a.SetFunc( MyFunc );
     a.Call();

     return 0;
}

AV MX Linux

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...