Skip to content

Instantly share code, notes, and snippets.

@guibou
Created July 14, 2021 19:46
Show Gist options
  • Save guibou/5a2780935dcbb1524b1ef178e3aa7141 to your computer and use it in GitHub Desktop.
Save guibou/5a2780935dcbb1524b1ef178e3aa7141 to your computer and use it in GitHub Desktop.
#include <iostream>
class Callback
{
public:
virtual void call() = 0;
};
Callback *registered_callback;
void register_callback(Callback *cb)
{
registered_callback = cb;
}
void call_registered_callbacks()
{
registered_callback->call();
}
class MyCallback : public Callback
{
private:
int value;
public:
MyCallback(int in_value):value(in_value){}
void call() override
{
std::cout << value << std::endl;
}
};
int main()
{
auto cb = new MyCallback{10};
register_callback(cb);
std::cout << "lalilala" << std::endl;
call_registered_callbacks();
}
#include <iostream>
void *cb_data;
void (*cb_f)(void*);
void register_callback(void (*f)(void*), void *data)
{
cb_data = data;
cb_f = f;
}
void call_registered_callbacks()
{
cb_f(cb_data);
}
void callback(void *data)
{
int *value;
value = (int*) data;
std::cout << *value << std::endl;
}
int main()
{
int payload = 10;
register_callback(&callback, &payload);
std::cout << "lalilala" << std::endl;
call_registered_callbacks();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment