[英]C++ Callback function, calling member variable inside static function
I've recently decided to start working with callback functions to make my life a bit easier. 我最近决定开始使用回调函数,以使我的生活更轻松。 However, I'm having a bit of an issue:
但是,我有一个问题:
#include <iostream>
#include <functional>
#include <memory>
class setCallback
{
public:
setCallback() {}
setCallback(std::function<int()> func) : callback(func) { }
int call() { return callback(); }
private:
std::function<int()> callback;
};
class callThis
{
public:
callThis() : number(10) {}
static int another()
{
return number;
}
int number;
};
int main(int argc, char** argv[])
{
std::shared_ptr<setCallback> caller;
std::shared_ptr<callThis> funcholder;
funcholder.reset(new callThis);
caller.reset(new setCallback(funcholder->another));
std::cout << caller->call() << "\n";
std::cin.get();
}
As you can see from the code, I pull in the function from "callthis" into "setCallBack". 从代码中可以看到,我将函数从“ callthis”插入“ setCallBack”。 This allows me to keep the function for later use, if needed.
这使我可以保留该功能,以备以后使用。 This works perfectly, however, if I attempt to load in a member variable from "callthis" into the static function, it obviously would not work (using a value does).
但是,如果我尝试将“ callthis”中的成员变量加载到静态函数中,这显然是行不通的(使用值可以)。
Is there a way around this? 有没有解决的办法? I would like to access the variable found within the class using the static function;
我想使用静态函数访问在类中找到的变量; optionally avoid using static functions when passing into the callback (but I assume this isn't possible due to the way callbacks work).
(可选)避免在传入回调时使用静态函数(但由于回调的工作方式,我认为这是不可能的)。
I've heard about wrapper classes but I'm not entirely sure how I would go about implementing it. 我听说过包装器类,但是我不确定如何实现它。 Some insight would be appreciated.
一些见识将不胜感激。
Thanks a lot. 非常感谢。
Edit: 编辑:
Solved calling variables with callback functions, by B Sahu 用B Sahu解决了带有回调函数的调用变量
Solved calling functions using callbacks without making the function being called static and keeping things reasonably uncoupled 使用回调解决了调用函数,而无需使该函数被静态调用并保持合理的解耦
I researched how to call non-static functions using C++11; 我研究了如何使用C ++ 11调用非静态函数。 I thought it might be useful if anyone ends up having the same issues I do.
我认为,如果有人最终遇到与我相同的问题,这可能会很有用。 This works on the same principles as R Sahu proposed, however, using types and procedures found in it ends up being a bit cleaner.
这与R Sahu提出的原理相同,但是,使用其中找到的类型和过程最终会更干净。 Using std::bind(class::func, object) you can effectively gain the same results.
使用std :: bind(class :: func,object)可以有效地获得相同的结果。
int main(int argc, char** argv)
{
std::function<void(void)> somefunc;
Functionholder func;
somefunc = std::bind(&Functionholder::say, &func);
}
Credit for this goes to: This beautiful person 这归功于: 这个美丽的人
Which when used in a more complex example, having a class which holds the function to call, a caller class which will take the function and call it when required and a binder/state class (note, i'm using this for game UI), you can really get some reasonably neat and uncoupled code going: 在更复杂的示例中使用时,该类具有一个包含要调用的函数的类,一个将使用该函数并在需要时调用它的调用者类以及一个绑定器/状态类(请注意,我将其用于游戏UI) ,您实际上可以得到一些合理整齐且未耦合的代码:
#include <iostream>
#include <functional>
class Functionholder
{
public:
void say() { std::cout << "hello!\n"; }
};
class CallbackHandler
{
public:
CallbackHandler(){}
CallbackHandler(std::function<void()> call) { callback = call; }
void update()
{
//something happens to call this
callback();
}
std::function<void()> callback;
};
class TheState
{
public:
TheState()
{
hand = CallbackHandler(std::bind(&Functionholder::say, &func));
}
void update() { hand.update(); }
Functionholder func;
CallbackHandler hand;
};
int main(int argc, char** argv)
{
TheState state;
state.update();
std::function<void(void)> somefunc;
std::cin.get();
}
If you have any improvements let me know! 如果您有任何改进,请告诉我!
Provide the ability to call the callback functions with user specified data. 提供使用用户指定的数据调用回调函数的功能。 Then, use the data in the callback function.
然后,使用回调函数中的数据。
#include <iostream>
#include <functional>
#include <memory>
class setCallback
{
public:
setCallback() {}
setCallback(std::function<int(void*)> func) : callback(func) { }
int call(void* data) { return callback(data); }
private:
std::function<int(void*)> callback;
};
class callThis
{
public:
callThis() : number(10) {}
static int another(void* data)
{
callThis* ptr = static_cast<callThis*>(data);
return ptr->number;
}
int number;
};
// Fixed this line. You had
// int main(int argc, char** argv[])
int main(int argc, char** argv)
{
std::shared_ptr<setCallback> caller;
std::shared_ptr<callThis> funcholder;
funcholder.reset(new callThis);
caller.reset(new setCallback(callThis::another));
std::cout << caller->call(funcholder.get()) << "\n";
std::cin.get();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.