C ++:将静态类成员与静态类成员的传递版本进行比较

时间:2013-12-09 21:06:00

标签: c++ class static

我有几个非常相似的类,可以互换地传递给一个函数。对于一个函数,它使用稍微不同的参数运行,因此我必须检查传递了哪个函数。目前我正在使用if语句,但收到错误

 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 ||

这是类声明和我的代码的开头:

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.;

更新::

我正在尝试使用

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

为每个功能。仅限于Meth,(不是陷阱等)我收到错误消息

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给了我错误:static static_cast

1 个答案:

答案 0 :(得分:2)

错误告诉您正在使用指向类方法的指针来比较指向全局函数的指针。它们是不同的类型。

编辑:类静态方法的指针与指向全局函数的指针具有相同的类型。因此,Trap的类型为double (*)(double (*)(double), int, double, double, bool)而非double (Integrators::*)(double (*)(double), int, double, double, bool)