简体   繁体   English

指向类成员函数的指针

[英]Pointer to refer to a member function of a class

Essentially what I want is to pass a reference of a member function into another function of another class. 本质上,我想要的是将成员函数的引用传递到另一个类的另一个函数中。 A sample code is like this: 示例代码如下:

#include<iostream>

using namespace std;

class algorithm
{
    void (*ODE)(double * timeDeri, double * var);
    void simulation()
    {
        //use ODE to do simulation
    }
    parent(void (*someODE)(double * timeDeri, double * var))
    {
        ODE=someODE;
    }
}

class model:algorithm
{
    void specificODE(double * timeDeri, double * var)
    {
        //detail of the ODE
    }
    model() : algorithm(specificODE)
    {
        //some initialization
    }
}

int main()
{
    model a;
    a.simulation();
}

Now I know this code won't work. 现在我知道此代码将无法工作。 Because function pointer can only be used to point on static function. 因为函数指针只能用于指向静态函数。 However, for some practical reasons, I cannot make specificODE static. 但是,出于某些实际原因,我无法使specificODE静态化。 So I wonder if there is a kind of pointer can help me do this. 所以我想知道是否有一种指针可以帮助我做到这一点。 Could anyone help? 有人可以帮忙吗?

PS1, I've checked on member function pointer. PS1,我检查了成员函数指针。 That pointer is useful if what I want is to point to a member function in the same class. 如果我要指向同一类中的成员函数,则该指针很有用。 However in this case, the pointer should be able to point to a member function of a different class. 但是,在这种情况下,指针应该能够指向不同类的成员函数。

PS2, Define algorithm class based on the model is a work around. PS2,基于模型定义算法类是一种变通方法。 However, I want to make model and algorithm independent to each other. 但是,我想使模型和算法彼此独立。 So that if I use the same algorithm on another model, I don't have to mess with the code in my algorithm class. 这样,如果我在另一个模型上使用相同的算法,就不必弄乱算法类中的代码。 For the same reason, I don't want to befriend the model class and the algorithm class or do anything specifically for the model class in the algorithm class. 出于同样的原因,我不想成为模型类和算法类的朋友,也不想为算法类中的模型类做任何事情。

The correct syntax for member function is 成员函数的正确语法是

void (algorithm::*ODE)(double * timeDeri, double * var)

As you mention, you need in fact be generic and use: 正如您提到的,实际上您需要通用并使用:

void (T::*ODE)(double * timeDeri, double * var)

So you may use CRTP for that: 因此,您可以为此使用CRTP:

template <typename T>
class algorithm
{
public:
    // You may use typedef:
    // typedef void (T::*ODE_T)(double * timeDeri, double * var);
    // ODE_T ODE;

    void (T::*ODE)(double * timeDeri, double * var);

    void simulation()
    {
        double d1, d2;

        (static_cast<T*>(this)->*ODE)(&d1, &d2);
        //use ODE to do simulation
    }

    //explicit algorithm(ODE_T someODE)
    explicit algorithm(void (T::*someODE)(double * timeDeri, double * var))
    {
        ODE = someODE;
    }
};

class model : private algorithm<model>
{
    friend algorithm<model>; // As you inherit privately from algorithm<T>.
public:
    void specificODE(double * timeDeri, double * var)
    {
        //detail of the ODE
    }
    model() : algorithm(&model::specificODE)
    {
        //some initialization
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM