如何在C中将函数作为参数传递?我想要创建一个函数,该函数对一组数据执行由参数传递的函数。如何在C中将函数作为参数传递?
3 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
typedef void (*functiontype)();
void dosomething() { }functiontype func = &dosomething;func();
typedef int (*functiontype2)(char);
int dosomethingwithchar(char a) { return 1; }functiontype2 func2 = &dosomethingwithcharint result = func2('a');
boost::function<int (char a)> functiontype2;
精慕HU
TA贡献1845条经验 获得超8个赞
std::function<bool (int)>
bool
int
.
// g++ test.cpp --std=c++11#include <functional>double Combiner(double a, double b, std::function<double (double,double)> func){ return func(a,b);}double Add(double a, double b){ return a+b;}double Mult(double a, double b){ return a*b;}int main(){ Combiner(12,13,Add); Combiner(12,13,Mult);}
// g++ test.cpp --std=c++11template<class T>double Combiner(double a, double b, T func){ return func(a,b);}double Add(double a, double b){ return a+b;}double Mult(double a, double b){ return a*b;}int main(){ Combiner(12,13,Add); Combiner(12,13,Mult);}
- 3 回答
- 0 关注
- 1632 浏览
添加回答
举报
0/150
提交
取消