假设我有两个不同的类,如下所示
class Nonlinear
{
public:
double NonlinearFunc(ParameterList) //it is a nonlinear function
};
class Solver
{
public:
double FuncEvaluation(ParameterList) //evaluate a function
};
double Solver::FuncEvaluation(ParameterList)
{
????? //evaluate function from other class
}
main()
{
Nonlinear nl;
Solver solve;
double a;
double b;
a = nl.NonlinearFunc(ParameterList);
b = solve.FuncEvaluation(ParameterList);
//what I want is a=b
}
如何使用成员函数指针,以便Solver::FunEvaluation评估Nonlinear::NonlinearFunc?
最佳答案
听起来您想让Solver::FuncEvaluation()
执行Nonlinear::NonlinearFunc()
,对吗?如果是这样,我会这样做:
class Evaluatable
{
public:
virtual double Evaluate(ParameterList) = 0;
};
class Nonlinear : public Evaluatable
{
public:
double Evaluate(ParameterList);
};
class Solver
{
public:
double FuncEvaluation(Evaluatable &expression, ParameterList);
};
double Solver::FuncEvaluation(Evaluatable &expression, ParameterList params)
{
return expression.Evaluate(params);
}
main()
{
Nonlinear nl;
Solver solve;
double a;
a = solve.FuncEvaluation(nl, ParameterList);
}
关于c++ - C++移植其他类的成员函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25901545/