所以,我想传递函数,即engine.cpp文件中的函数,作为参数,以及我所做的:
typedef RFun(*wsk)(double, int);
RFun Engine::f_line(double *&values, int howmany)
{
RFun line;
for(int i = 0; i < howmany; i++)
{
line.result_values[i] = (2 * values[i]) + 6;
}
return line;
}
RFun counter(double *&values, int howmany, wsk function)
{
return function(*values, howmany);
}
现在我想在其他.cpp文件中调用计数器函数并将f_line函数作为参数传递给内部。我怎样才能做到这一点?
答案 0 :(得分:2)
这是一个简单的example如何使用std :: function。
#include <iostream>
#include <functional>
using namespace std;
void func1()
{
// a function that takes no parameters and does nothing
cout << "in global func1" << endl;
}
class Example
{
public:
int value;
void memberfunc()
{
cout << "in memberfunc. value=" << value << endl;
}
};
void CallAFunction( std::function< void() > functocall )
{
functocall(); // call it normally
}
int main()
{
// call a global function
CallAFunction( func1 ); // prints "in global func1"
// call a member function (a little more complicated):
Example e;
e.value = 10;
CallAFunction( std::bind( &Example::memberfunc, std::ref(e) ) );
// prints "in memberfunc. value=10"
}
尝试here
成功时间:0记忆:15240信号:0
在全局func1中 在memberfunc中。值= 10