C++;信号回调(如javascript) 我是C++新手,我在学习新语言的同时尝试移植一些JavaScript脚本。
我正试图找到解决方案来使用像javascript这样的回调,特别是像库这样的回调 下面是一个javascript脚本。它能转换成C++吗?怎么用?如果没有,最好的解决方案是什么 JavascriptC++;信号回调(如javascript) 我是C++新手,我在学习新语言的同时尝试移植一些JavaScript脚本。,javascript,c++,function,callback,signals,Javascript,C++,Function,Callback,Signals,我正试图找到解决方案来使用像javascript这样的回调,特别是像库这样的回调 下面是一个javascript脚本。它能转换成C++吗?怎么用?如果没有,最好的解决方案是什么 Javascript var ns = { _callback: null, setUpdate: function(callback) { ns._callback = callback; }, update: function() { // do some default
var ns = {
_callback: null,
setUpdate: function(callback) {
ns._callback = callback;
},
update: function() {
// do some default things
ns._callback();
}
};
ns.setUpdate(function() {
console.log("I'm Changed"); // will be: std::cout << "I'm Changed\n";
});
我不会使用这样的
命名空间。类
可能更适合以下情况:
#include <iostream>
#include <functional>
class MyThing
{
public:
MyThing() {}
MyThing(const std::function<void()>& callback) : callback_(callback) {}
void setUpdate(const std::function<void()>& callback)
{
callback_ = callback;
}
void update()
{
std::cout << "Update called\n";
if (callback_) {
callback_();
}
}
private:
std::function<void()> callback_;
};
int main()
{
MyThing my_thing1; // callback_ is initially unset in this object
my_thing1.update(); // no callback is called
my_thing1.setUpdate([](){ std::cout << "I'm Changed 1\n"; });
my_thing1.update(); // calls the lambda set in the previous line
MyThing my_thing2([](){ std::cout << "I'm Changed 2\n"; });
my_thing2.update(); // calls the lambda in the prevous line
}
#包括
#包括
阶级神话
{
公众:
神话(){}
虚构(const std::function&callback):回调(callback){}
无效设置日期(const std::函数和回调)
{
回调=回调;
}
无效更新()
{
std::cout我不会使用这样的名称空间。类可能更适合这种情况:
#include <iostream>
#include <functional>
class MyThing
{
public:
MyThing() {}
MyThing(const std::function<void()>& callback) : callback_(callback) {}
void setUpdate(const std::function<void()>& callback)
{
callback_ = callback;
}
void update()
{
std::cout << "Update called\n";
if (callback_) {
callback_();
}
}
private:
std::function<void()> callback_;
};
int main()
{
MyThing my_thing1; // callback_ is initially unset in this object
my_thing1.update(); // no callback is called
my_thing1.setUpdate([](){ std::cout << "I'm Changed 1\n"; });
my_thing1.update(); // calls the lambda set in the previous line
MyThing my_thing2([](){ std::cout << "I'm Changed 2\n"; });
my_thing2.update(); // calls the lambda in the prevous line
}
#包括
#包括
阶级神话
{
公众:
神话(){}
虚构(const std::function&callback):回调(callback){}
无效设置日期(const std::函数和回调)
{
回调=回调;
}
无效更新()
{
std::cout您可以使用函数指针,如下所示:
#include <cstdio>
namespace ns {
// defines a type named Callback that is a pointer
// to a function that takes void and returns void
typedef void (*Callback)();
Callback _callback;
void setUpdate( Callback cb) {
_callback = cb;
}
void update() {
_callback();
}
}
void MyUpdate() {
printf("hello from callback\n");
}
int main(int argc, char* argv[]) {
ns::setUpdate(MyUpdate);
ns::update();
}
#包括
名称空间ns{
//定义一个名为Callback的类型,该类型是指针
//对于接受void并返回void的函数
typedef void(*回调)();
回调(Callback);;
无效设置日期(回调cb){
_回调=cb;
}
无效更新(){
_回调();
}
}
void MyUpdate(){
printf(“来自回调的hello\n”);
}
int main(int argc,char*argv[]){
ns::设置日期(MyUpdate);
ns::update();
}
输出:
你好,回拨
另请参见:您可以使用函数指针,如下所示:
#include <cstdio>
namespace ns {
// defines a type named Callback that is a pointer
// to a function that takes void and returns void
typedef void (*Callback)();
Callback _callback;
void setUpdate( Callback cb) {
_callback = cb;
}
void update() {
_callback();
}
}
void MyUpdate() {
printf("hello from callback\n");
}
int main(int argc, char* argv[]) {
ns::setUpdate(MyUpdate);
ns::update();
}
#包括
名称空间ns{
//定义一个名为Callback的类型,该类型是指针
//对于接受void并返回void的函数
typedef void(*回调)();
回调(Callback);;
无效设置日期(回调cb){
_回调=cb;
}
无效更新(){
_回调();
}
}
void MyUpdate(){
printf(“来自回调的hello\n”);
}
int main(int argc,char*argv[]){
ns::设置日期(MyUpdate);
ns::update();
}
输出:
你好,回拨
另请参见:这是一种将javascript js信号库移植到c++的好方法:
#include <string>
#include <map>
#include <functional>
// You only need this Signal class
class Signal {
// store all callbacks to a string map
std::map<std::string, std::function<void()>> callbacks;
// check if callback already exists. optional
bool exists(std::string key) {
if (callbacks.find(key) == callbacks.end()) {return false;}
else {return true;}
}
public:
void add(std::string key, std::function<void()> cb) {
if (!exists(key)) {remove(key);}
callbacks.insert(std::pair<std::string, std::function<void()>>(key, cb));
}
void remove(std::string key) {
callbacks.erase(key);
}
void dispatch() {
for (std::map<std::string, std::function<void()>>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
callbacks[it->first]();
}
}
// this destroys the signal
void dispose() {
callbacks.clear();
}
};
int main() {
// create a signal
Signal* onFire = new Signal();
// add some callbacks
int a_variable = 10;
onFire->add("a_sound_is_heard", [&]() {
// some code when onFire.dispatch() happens
std::cout << "i can use this variable too :) " << a_variable << std::endl;
// you can also use the onFire variable
});
onFire->add("a_bullet_appears", [&]() {
// some code again
});
// remove some callbacks (add a silencer for your gun)
onFire->remove("a_sound_is_heard");
// whenever you want, execute the event
onFire->dispatch();
// don't forget to delete the signal from memory
onFire->dispose();
delete onFire;
return 0;
}
#包括
#包括
#包括
//你只需要这个信号类
类信号{
//将所有回调存储到字符串映射
std::map回调;
//检查回调是否已存在。可选
bool存在(标准::字符串键){
if(callbacks.find(key)=callbacks.end()){return false;}
else{return true;}
}
公众:
void add(标准::字符串键,标准::函数cb){
如果(!exists(key)){remove(key);}
insert(std::pair(key,cb));
}
无效删除(标准::字符串键){
回调。擦除(键);
}
无效分派(){
for(std::map::iterator it=callbacks.begin();it!=callbacks.end();it++){
回调[it->first]();
}
}
//这会破坏信号
无效处置(){
callbacks.clear();
}
};
int main(){
//发出信号
信号*onFire=新信号();
//添加一些回调
int a_变量=10;
onFire->add(“听到声音,[&](){
//发生onFire.dispatch()时的一些代码
std::cout dispatch();
//别忘了从存储器中删除信号
onFire->dispose();
删除onFire;
返回0;
}
这是一种将javascript js信号库移植到c++的好方法:
#include <string>
#include <map>
#include <functional>
// You only need this Signal class
class Signal {
// store all callbacks to a string map
std::map<std::string, std::function<void()>> callbacks;
// check if callback already exists. optional
bool exists(std::string key) {
if (callbacks.find(key) == callbacks.end()) {return false;}
else {return true;}
}
public:
void add(std::string key, std::function<void()> cb) {
if (!exists(key)) {remove(key);}
callbacks.insert(std::pair<std::string, std::function<void()>>(key, cb));
}
void remove(std::string key) {
callbacks.erase(key);
}
void dispatch() {
for (std::map<std::string, std::function<void()>>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
callbacks[it->first]();
}
}
// this destroys the signal
void dispose() {
callbacks.clear();
}
};
int main() {
// create a signal
Signal* onFire = new Signal();
// add some callbacks
int a_variable = 10;
onFire->add("a_sound_is_heard", [&]() {
// some code when onFire.dispatch() happens
std::cout << "i can use this variable too :) " << a_variable << std::endl;
// you can also use the onFire variable
});
onFire->add("a_bullet_appears", [&]() {
// some code again
});
// remove some callbacks (add a silencer for your gun)
onFire->remove("a_sound_is_heard");
// whenever you want, execute the event
onFire->dispatch();
// don't forget to delete the signal from memory
onFire->dispose();
delete onFire;
return 0;
}
#包括
#包括
#包括
//你只需要这个信号类
类信号{
//将所有回调存储到字符串映射
std::map回调;
//检查回调是否已存在。可选
bool存在(标准::字符串键){
if(callbacks.find(key)=callbacks.end()){return false;}
else{return true;}
}
公众:
void add(标准::字符串键,标准::函数cb){
如果(!exists(key)){remove(key);}
insert(std::pair(key,cb));
}
无效删除(标准::字符串键){
回调。擦除(键);
}
无效分派(){
for(std::map::iterator it=callbacks.begin();it!=callbacks.end();it++){
回调[it->first]();
}
}
//这会破坏信号
无效处置(){
callbacks.clear();
}
};
int main(){
//发出信号
信号*onFire=新信号();
//添加一些回调
int a_变量=10;
onFire->add(“听到声音,[&](){
//发生onFire.dispatch()时的一些代码
std::cout dispatch();
//别忘了从存储器中删除信号
onFire->dispose();
删除onFire;
返回0;
}
是的,你可以在C++中做回调。我认为最接近C++的JS代码(我不知道JS,所以我只是猜测)是在一个<代码> STD::函数< /COD>成员变量中存储lambda。搜索“函数指针C++”和“lambda表达式C++”。……这将使你开始……有很多事情要处理,你可以在C++中做回调。我认为最接近C++的JS代码(我不知道JS,所以我只是猜测)是在一个<代码> STD::函数< /COD>成员变量中存储lambda。搜索“函数指针C++”,“lambda expressions c++”…这会让你开始…有很多东西需要处理,谢谢。你能解释一下它的作用吗?“typedef void(*Callback)(;”在代码中添加了一条注释来解释..我在原始帖子中的链接也解释了更多。非常感谢。你能解释一下它的作用吗?“typedef void(*Callback)();“在代码中添加了一条注释来解释..我在原始帖子中的链接也解释了更多。