Search code examples
c++c++11functor

Using a functor inside member functions of another class


I have a functor class with an internal state and fixed output type and fixed parameters necessary for constructing the object:

class Functor
{
    public:
        /* constructor */
        Functor(double var1, double var2 ...)
        {
            /* some initialization of internal variables */
        }

        array<double,N> operator()(double par1, ...)
        {
            array<double,N> output;
            /* some calculations using private member functions */
            return output;
        }

private:
    /* internal variables */
    double internal_var1;
    ...

    /* internal functions */
    double internal func1(double var1, ...)
    {
        /* calculation */

    }
    ...
};

This functor is instantiated in the main program using input parameters from the user. I want to use this functor inside the member functions of other classes, which are also functors, for further calulations. One important aspect specific to this question is, that these functors use a specific signature that i cannot alter, otherwise i would just provide these functors with the result of the initial functor (i.e. the one of class Functor) as input parameters when calling them.

My idea so far (which quickly turned out to be nonsense) was to have these classes have a member that is a pointer to a class of the aforementioned functor and provide the constructor of these classes a reference to the functor:

class C1
{
    public:
        /* constructor */
        C1(/* some parameters */, Functor* functor) // this is probably nonsense
        {
            /* C1 member initialization */
            ... 
            functor_ptr = functor;
        }

        void operator()(/* !!! fixed parameter signature here !!! */)
        {
            /* calulations using internal functions... */
        }

    private:
        /* member variables and the functor class pointer*/
        double internal_var1;
        ... etc. ...
        Functor* functor_ptr;

        /* member functions */
        double internal_func1(double par1, ...)
        {
            /* use the functor */
            double<array,N> tmp = (*functor_ptr)(par1, par2, ...) // more nonsense
            /* more calculations */
            return result;
        }
        double internal_func2(...)
        ... etc. ...
};

From what i looked up so far it seems that using a std:function call inside C1 could achieve what i'm trying to do (and i can use c++11). This post seems very similar to what i want, however, i can't figure out how to attach my functor to the std::function call as my C(ung)-fu is still rather weak. Also i couldn't figure out if it is possible to have something like std:function<array<double,N>(double,double,...> call_functor as a member of a class , which is the initialized in the constructor.

Can, and if yes how, this be done using std::function or is there a better way?


Solution

  • Indeed, return a function with std:function<array<double,N>(double,double,...)>, and to create it use a lambda:

    std:function<array<double,N>(double,double,...)>([this](double x, double y, ...){return this->function(x, y,);};
    

    You need to capture this of course to know on which object the method should be called.