1

I'm trying to make a code that find the numerical derivation of a function. I also have a polynomial class described as follows:

    class polynomial
    {
    public:
        polynomial(Vector, int);
        polynomial();
        ~polynomial();

        double returnValue(double);
        void print();

    private:
        int Degree;
        Vector Coeficients;
    };

my numerical derivation have the following prototype:

 double numericalDerivation( double (*F) (double), double x);

I want to pass the returnValue method into the numericalDerivation, is that possible?

4

1 回答 1

0

是的,这是可能的,但不要忘记它是一个成员函数:如果没有(指向)用于调用它的类型对象,您将无法polynomial调用它。

以下是您的函数的签名应如下所示:

double numericalDerivation(double (polynomial::*F)(double), polynomial* p, double x)
{
    ...
    (p->*F)(x);
    ...
}

以下是调用它的方法:

double d = ...;
polynomial p;
numericalDerivation(&polynomial::returnValue, &p, d);

或者,您可以使用一个std::function<>对象作为函数的参数,然后std::bind()将对象绑定到成员函数:

#include <functional>

double numericalDerivation(std::function<double(double)> f, double x)
{
    ...
    f(x);
    ...
}

...

double d = ...;
polynomial p;
numericalDerivation(std::bind(&polynomial::returnValue, p, std::placeholders::_1), d);
于 2013-02-19T01:49:47.497 回答