Using a functor inside member functions of another class












0















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?










share|improve this question





























    0















    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?










    share|improve this question



























      0












      0








      0








      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?










      share|improve this question
















      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?







      c++ c++11 functor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 10:16







      Lt. Frank Drebin

















      asked Nov 19 '18 at 10:12









      Lt. Frank DrebinLt. Frank Drebin

      265




      265
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer



















          • 1





            yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

            – Lt. Frank Drebin
            Nov 27 '18 at 12:19













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372384%2fusing-a-functor-inside-member-functions-of-another-class%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.






          share|improve this answer



















          • 1





            yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

            – Lt. Frank Drebin
            Nov 27 '18 at 12:19


















          0














          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.






          share|improve this answer



















          • 1





            yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

            – Lt. Frank Drebin
            Nov 27 '18 at 12:19
















          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 10:15









          Matthieu BrucherMatthieu Brucher

          15.7k32140




          15.7k32140








          • 1





            yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

            – Lt. Frank Drebin
            Nov 27 '18 at 12:19
















          • 1





            yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

            – Lt. Frank Drebin
            Nov 27 '18 at 12:19










          1




          1





          yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

          – Lt. Frank Drebin
          Nov 27 '18 at 12:19







          yes, this works, thanks.. sorry for the delay on marking this as an answer.. also after fixing some other bug in my code, it seems that my of using a functor pointer as posted in the question seems to work also..

          – Lt. Frank Drebin
          Nov 27 '18 at 12:19






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372384%2fusing-a-functor-inside-member-functions-of-another-class%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()