我在使用C ++中的这种回调实现时遇到了麻烦。在下面的代码中,我想将回调存储到“ OnRenderingComplete”中。但是,我很难记住“ adOnRenderingComplete”的“ ad”变量和“ callback”参数。
如何用C ++实现?
var adOnRenderingComplete = function(ad, callback) {
};
var setAdPlaybackCallbacks = function(ad, callback) {
OnRenderingComplete = function() { adOnRenderingComplete.call(this, ad, callback); };
};
在JavaScript中,这似乎是可行的,因为可以有一个嵌入式函数来存储“ ad”和“ callback”的参数...但是我不确定在C ++中如何实现。我最终想调用OnRenderingComplete(),并用它“记住”参数“ ad”和“ callback”。
我很难过我似乎无法弄清楚。我唯一想到的就是在函数中定义一个类并将该类的函数传递给OnRenderingComplete。但是即使如此,下面的内容也是错误的并且无法编译。
void testCall(void (*callback)())
{
callback();
}
void test1()
{
class blah
{
public:
int a;
int b;
void c()
{
cout << "a * b = " << a*b << endl;
};
};
blah ex;
ex.a = 5;
ex.b = 3;
void(*OnRenderingComplete)() = ex.c;
testCall(OnRenderingComplete); // I would like to have this print the value of a*b
}
本质上,我试图在回调变量“ OnRenderingComplete”内捕获“ ex.c”。但是,与此同时,我想捕获ex.a和ex.b的值,以便可以在“记住”“ a”和“ b”的数据的同时调用ex.c。在调用“ testCall(OnRenderingComplete)”之后,我希望顶部的testCall()函数能够打印a * b的值。
最佳答案
几种解决方案(单击标题获得实时示例):
C++11:
#include <functional>
struct MyClass
{
typedef std::function<void (int, bool)> CallbackType;
typedef std::function<void (int, float, bool)> AnotherCallbackType;
CallbackType callback;
void setCallback(AnotherCallbackType c, float param)
{
callback = [&](int a, bool b) { c(a, param, b); };
// Calling the callback:
callback(42, false);
// With lambdas, it is almost like in Javascript,
// you can embed all parameters you want. ex:
std::function<void()> func = [&](){ c(2, param, true); };
func(); // will call c(2, param, true);
}
};
C++03, with Boost:
#include <boost/function.hpp>
#include <boost/bind.hpp>
struct MyClass
{
typedef boost::function<void (int, bool)> CallbackType;
typedef boost::function<void (int, float, bool)> AnotherCallbackType;
CallbackType callback;
void setCallback(AnotherCallbackType c, float param)
{
callback = boost::bind(c, _1, param, _2);
// Calling the callback:
callback(42, false);
// This will call: c(42, param, false)
// The 'param' float parameter has been retained.
// It is possible to retain all parameters when binding, ex:
boost::function<void()> func = boost::bind(c, 2, param, true);
func(); // will call c(2, param, true);
}
};
C++03, without Boost:
struct MyClass
{
typedef void (MyClass::* CallbackType)(int, float);
CallbackType callback;
void onRenderCallback(int param1, float param2) {}
void setCallback(CallbackType c)
{
callback = c;
// Calling the callback:
(this->*callback)(42, 3.14f);
// Note that it is possible to embed the parameters
// but that will need to create a class, basically it means you will reimplement Boost.Bind...
}
};
现在,回到您的代码:
void(*OnRenderingComplete)() = ex.c;
对于函数指针,我强烈建议您使用typedefs:
typedef void (*OnRenderingCompleteType)();
OnRenderingCompleteType callback = ...;
然后,
ex.c
具有以下类型:void (blah::*)()
。注意blah::*
,因为这是指向成员函数的指针,而不是指向函数的指针(成员函数是特殊函数)。这就是分配无法编译的原因。关于javascript - 模仿C++中的特殊JavaScript回调,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21278944/