Reputation: 5642
How would I achieve the following JavaScript implementation of callbacks in C++? Below is taken from a JavaScript snippet. I'd like to achieve the same functionality:
var playAdBreak = function(adbreak, callback)
{
var x;
playSingle(x, function() { playAdBreak.call(this, adbreak, callback); });
};
var playSingle = function(abc, callback)
{
};
Upvotes: 0
Views: 177
Reputation: 325
The modest way of doing it that I can think is
void callBackDefinition()
{
printf("I was called");
}
void callBackExecutor(void (*callBack))
{
callBack();
}
main()
{
callBackExecutor(&callBackDefinition);
return 0;
}
Upvotes: 1
Reputation: 108
If you are under C++11 environment, you can use lambda, std::function
. Below code doesn't have same functionality, but it can convert into like below except for : C++ lambda is not closure and there is no this
for a function.
auto playSingle = [&](const T &abc, std::function<void()> callback) {
}
auto playAdBreak = [&](const Y &adbreak, std::function<void()) callback) {
T x;
playSingle(x, [=]() { playAdBreak(adbreak, callback); });
}
Upvotes: 1