ParaToxic Posted December 21, 2012 Share Posted December 21, 2012 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 ? Quote Link to comment Share on other sites More sharing options...
Rick Posted December 21, 2012 Share Posted December 21, 2012 http://www.leadwerks.com/werkspace/files/file/367-c-event/ Does nobody look at the assets store Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted December 21, 2012 Author Share Posted December 21, 2012 Well thanks for the suggestion but I have a event system already done, but this command don't use the events/signals for that. Maybe somebody can tell me the correct C++ way of entering a class own function to this function pointer. Quote Link to comment Share on other sites More sharing options...
Rick Posted December 21, 2012 Share Posted December 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted December 21, 2012 Author Share Posted December 21, 2012 Okey thanks ;D Will implement that Quote Link to comment Share on other sites More sharing options...
Roland Posted December 21, 2012 Share Posted December 21, 2012 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; } Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
ParaToxic Posted December 21, 2012 Author Share Posted December 21, 2012 Hei Roland, the stuff you have posted is exactly the way I do it in my class. But.. You have MyFunc outside a class ,so it works fine but when you want to use a function of an object it doesn't work. That was the problem but I fixed it without a function pointer. Thanks 1 Quote Link to comment Share on other sites More sharing options...
Roland Posted December 21, 2012 Share Posted December 21, 2012 Okay. Great. Yes, a solution without function pointers would have been my first choice also. 1 Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Recommended Posts
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.