C++ 切换功能作为具有参数的参数
我想传递一个包含参数的回调函数:C++ 切换功能作为具有参数的参数,c++,C++,我想传递一个包含参数的回调函数: class foo1{ foo1(void (*callback)(float)); }; foo1::foo1(void (*callback)(float)){ //excecute the callback at some point } float foo2(){ return 1.1; } void foo3(float f){ //do stuff with f return; } int main(){
class foo1{
foo1(void (*callback)(float));
};
foo1::foo1(void (*callback)(float)){
//excecute the callback at some point
}
float foo2(){
return 1.1;
}
void foo3(float f){
//do stuff with f
return;
}
int main(){
void (*p3)(float);
//p3 is a pointer to a function that returns void and has a single float as input
p3 = &foo3(foo2());
//p3 now points to foo3 wich fits the requirements. But it does not make sence to give that pointer an argument.
foo1(p3);
return 0;
}
有几个错误和错误
我明白这没有意义。(参见代码中的注释)但我不知道如何正确地执行。我想以回调的形式传递一个输入值为foo2的函数。您可以使用lambda来完成此操作。
沿着这条路走下去应该是可行的:
struct foo1{
template<typename F>
foo1(F f) {
//excecute the callback at some point
f();
}
};
float foo2(){
return 1.1;
}
void foo3(float){
//do stuff with f
return;
}
int main(){
foo1([param{foo2()}](){ foo3(param); });
}
它创建了一个类型为void(void)
的可调用对象,这是您通过在foo3
的第一个参数处应用执行foo2
的结果所期望的(对吗?)。这就是为什么您可以在
foo1
的构造函数中将其作为f()
调用的原因,您可以执行类似的操作:
class foo1 {
foo1(void (*callback)(float));
};
如果不希望foo1()
将参数值传递给回调,则不要以输入参数开始声明回调,请使用另一个调用预期回调函数的回调函数:
class foo1 {
foo1(void (*callback)());
};
foo1::foo1(void (*callback)()) {
//excecute the callback at some point
callback();
}
或者,在C++11及更高版本中,可以使用lambda:
class foo1 {
template<class T>
foo1(T callback) {
//excecute the callback at some point
callback();
}
};
Or:
#include <functional>
class foo1 {
foo1(std::function<void()> callback) {
//excecute the callback at some point
callback();
}
};
或者,您可以使用:
1类{
模板
foo1(T回调){
//在某个时候执行回调
回调();
}
};
或:
#包括
foo1类{
foo1(std::函数回调){
//在某个时候执行回调
回调();
}
};
#包括
浮点数2(){
返回1.1;
}
无效foo3(浮动f){
//用f做东西
}
int main(){
foo1(std::bind(foo3,foo2());
返回0;
}
有没有办法让foo1成为一个类?我需要创建it@user7408924foo1
是一个类。如果您更喜欢使用class
关键字,请使用以下内容:class foo1{public://*您可以在示例中看到的相同主体*/}代码>。我不熟悉这种语法。“使用这个:…/*相同的主体…*/”的意思是:classfoo1{foo1(void(*callback)(float));};模板foo1::foo1(F){//在某个点执行回调}
?编辑:当然,foo1的声明也应该有类型(F)作为输入…@user7408924否,这:classfoo1{public:template foo1(F){//在某个点F()执行回调;}代码>。如果我将其放入原始代码中,我会得到它要求我包含的错误。我不认为初始值设定项列表是一个真正的库(找不到它)。你知道这意味着什么吗?谢谢你的详细回答!既然你们两个(@skypjack)都推荐了lambda方式,我现在就试试那个。我现在得到了错误error:expected type specifier在'foo1'之前另一个foo(新的foo1(其他参数,[]){
它也在arduino ide中编译。因此,错误一定在我的实际代码中更深一些。我想我的问题已经得到了回答。谢谢
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
int main() {
void (*p3)(float);
p3 = &foo3;
foo1(p3);
// or simply:
// foo1(&foo3);
return 0;
}
class foo1 {
foo1(void (*callback)());
};
foo1::foo1(void (*callback)()) {
//excecute the callback at some point
callback();
}
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
void foo4() {
foo3(foo2());
}
int main() {
void (*p4)();
p4 = &foo4;
foo1(p4);
// or simply:
// foo1(&foo4);
return 0;
}
class foo1 {
template<class T>
foo1(T callback) {
//excecute the callback at some point
callback();
}
};
Or:
#include <functional>
class foo1 {
foo1(std::function<void()> callback) {
//excecute the callback at some point
callback();
}
};
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
int main() {
foo1([](){ foo3(foo2()); });
return 0;
}
class foo1 {
template <typename T>
foo1(T callback) {
//excecute the callback at some point
callback();
}
};
or:
#include <functional>
class foo1 {
foo1(std::function<void()> callback) {
//excecute the callback at some point
callback();
}
};
#include <functional>
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
int main() {
foo1(std::bind(foo3, foo2()));
return 0;
}