albiero
albiero

Reputation: 91

C++: Comparing static class member to a passed version of the static class member

I have several very similar functions of the same class that can be interchangeably passed to a function. For one function, it runs with a slightly different parameter, so I have to check which function was passed. Currently I'm using an if statement, but getting the error

 error: invalid operands of types ‘double (Integrators::*)(double (*)(double),
      int, double, double, bool)’ and ‘double (*)(double (*)(double), int, 
       double, double, bool)’ to binary ‘operator==’
            if(Meth==&Integrators::Trap || Meth==&Integrators::Simp ||

This is the class declaration and the start of my code:

class Integrators{
public:
    static double Trap(double(*f)(double),int N, double a, double b, 
        bool closed=true);
    static double Simp(double(*f)(double),int N, double a, double b, 
        bool closed=true);
    static double Midp(double(*f)(double),int N, double a, double b,
        bool closed=true);
    static double SInf(double(*f)(double),int N, double a, double b, 
        bool closed=true);
    double ToEps(double (Integrators::*Meth)(double
                    (double),int,double,double,bool), 
        double (*f)(double), double a, double b, 
                    double eps, int Jmax=100, int Jmin=3, 
                    bool closed=true);
};

double Integrators::ToEps(double (Integrators::*Meth)(double (double),int,double,
    double,bool), 
            double (*f)(double), double a, double b, double eps,
            int Jmax, int Jmin, bool closed)
{

    double fac;
    if(Meth==&Integrators::Trap || Meth==&Integrators::Simp ||
        Meth==&Integrators::SInf)
    fac=2.;
    else if(Meth==&Integrators::Midp)
    fac=3.;

Update::

I'm trying using

void * Pmeth=reinterpret_cast<void*>(Meth);

for each of the functions. Only for Meth, (not Trap, etc.) I am getting the error message

Integrators.cpp:216:43: warning: converting from ‘double (Integrators::*)
(double (*)  (double), int, double, double, bool)’ to ‘void*’ 
[-Wpmf-conversions]  void * Pmeth=reinterpret_cast<void*>(Meth);

static_cast was giving me error: invalid static_cast

Upvotes: 0

Views: 71

Answers (1)

bolov
bolov

Reputation: 75825

The error tells that you are comparing pointers to global functions with pointers to class methods. They are different types.

Edit: pointers to class static methods have the same type with pointers to global functions. So the type of Trap is double (*)(double (*)(double), int, double, double, bool) and not double (Integrators::*)(double (*)(double), int, double, double, bool)

Upvotes: 2

Related Questions