是否可以帮助我传递一个仿函数,例如:
struct BFunc
{
double operator()(const double x, const double y, const double z){
return 0.1;
}
};
到类的构造函数:
class foo
{
int n;
public:
foo();
foo(int nn, &bfunc):n(nn),f(func()){
}
double getResult(double x){
return f(x);
}
}
谢谢,
答案 0 :(得分:0)
谢谢大家的快速回复。我解决了它,通过声明类型functor的变量如下所示。但我想知道如果没有它可以做到这一点。关键是我想要创建一个类,我将一个函数作为边界条件和一个范围传递给它,因此它创建了一个2D或3D坐标矩阵,它给内部的所有点零,对外的所有点都为零。我想尽可能地让它变得一般。最终我最终遵循以下方法。
using namespace std;
struct functor {
int a=11;
int operator () (int x) const {
cout << a << endl;
return 2*x;
}
};
class myclass {
int y=0;
functor af;// ** I did not wanted to have this line and make it generic ** //
public:
template <typename F> myclass(const F & f):af(f){
//af=f;//auto af(f);
y=f(5);
cout<<"y is"<<y<<endl;
int z=af(5);
cout<<"z is"<<z<<endl;
cout<<" why "<<typeid(af).name()<<endl;
}
int getFuncX(int x){
int y=af(x);
return y;
}
};
int main(int argc, char **argv) {
functor f = {5};
myclass c(f);
cout<<" See What happens: "<<c.getFuncX(71)<<endl;
return 0;
}
抱歉,如果它很乱!正如你猜测我是初学者。 谢谢