[英]c++ callback of a member function
#include<iostream>
class Bar;
class Foo{
public:
void (Bar::*callback)(void);
Bar* bar;
Foo(Bar* bar, void (Bar::*cb)(void)){
callback = cb;
}
void execute(){
(bar->*callback); // commenting out this will make it compile
// i want to execute callback here, but can't find a way to do it
}
};
class Bar{
public:
Foo *f;
Bar(){
f = new Foo(this, &Bar::func);
f->execute();
}
void func(){ // this can't be static
std::cout << "func executed" << std::endl;
}
};
int main(){
Bar b;
return 0;
}
这是我想要做的,我需要对按钮进行回调。 但是我无法调用成员函数指针。
还是我宁愿使用另一种方式来获得此功能?
编辑:我得到的错误是“无效使用非静态成员函数”使函数静态不是一种选择。
(bar->*callback);
您不是在这里调用该函数,而只是取消引用它:
(bar->*callback)(); // ^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.