Kotov72rus Posted December 18, 2012 Share Posted December 18, 2012 Please help me with an example, to replace the function of the class - the user function. There is a class: class TGuiButton { private: int x,y,width,height; string title; int MouseIn(TVec2 start, TVec2 dimensions); bool IsClick; bool buf1,buf2; TEntity buf; void * TempFunction; //To call the user function. public: string SoundEnter; string SoundLeave; string SoundClick; string TextureUp; string TextureHover; string TextureDown; bool Sounds; bool Enabled; bool Visible; void Create(int _x, int _y, int _width, int _height, string _title); void onClick(void *callback); //Function for replace. void Update(); void Delete(); }; .... void TGuiButton::onClick(void *callback) { //????? } .... And call a user function with TempFunction; Quote Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 18, 2012 Author Share Posted December 18, 2012 The question is solved Class: void TempFunction(){} void (*TempCallback)(); void onClick(void (*callback)()); void TGuiButton::Create(...) { TempCallback = TempFunction; } void TGuiButton::onClick(void (*callback)()) { TempCallback = callback; //TempCallback(); } User function: void TestClick() { Terminate(); } .onClick(&TestClick); Quote Link to comment Share on other sites More sharing options...
Rick Posted December 18, 2012 Share Posted December 18, 2012 You might want to take a look at http://www.leadwerks...le/367-c-event/ Otherwise it looks like you have a C++ library but your event callbacks are normal C functions, which makes it awkward for people using your library. I mean if I'm using a C++ library I expect everything about it to be object oriented myself. That includes the event callbacks being able to be class methods and not normal C functions. 1 Quote Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 18, 2012 Author Share Posted December 18, 2012 Really very interesting, comfortable and functional. Thank you. Quote Link to comment Share on other sites More sharing options...
Rick Posted December 18, 2012 Share Posted December 18, 2012 Yeah, this event code really opens up doors when coding in C++. I use this all over the place. I didn't create this code, someone taught me a long time ago about it, but it's amazing and gives C++ a really nice event system. Hope it opens up a whole new world for you because it did to me 1 Quote Link to comment Share on other sites More sharing options...
ChrisMAN Posted December 18, 2012 Share Posted December 18, 2012 The reason events are so powerful is because they reverse the flow of dependencies. It might be kind of obvious to say such but there might be people who don't know why they are awesome. Quote 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.