Jump to content

Recommended Posts

Posted

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; sad.png

Posted

The question is solved laugh.png

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);

Posted

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.

  • Upvote 1
Posted

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 :)

  • Upvote 1
Posted

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.

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.

×
×
  • Create New...