home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,896 of 33,346   
   Edward Diener to Jerry   
   Re: currying pointer to member functions   
   07 Mar 13 05:36:53   
   
   d4526246   
   From: eldiener@tropicsoft.invalid   
      
   On 03/05/2013 06:20 PM, Jerry wrote:   
   > I appreciate any advice about how to do this.   
   >   
   > I can make this work:   
   >   
   > struct a   
   > {   
   >      int b;   
   >      int c() {return b+1;}   
   >      int d(int x) {return b+x;}   
   > };   
   >   
   > int main()   
   > {   
   >      a m = { 1 }, n = { 2 };   
   >      a *ps = &m;   
   >      int (a::*pf)() = &a::c;   
   >      std::cout << (ps->*pf)() << std::endl;   
   >      return 0;   
   > }   
   >   
   > And it runs the function and everything works.  But what I want to   
   > do is curry the function so that I can store (ps->*pf) and then   
   > later execute it.  So what is the type of &(ps->*pf) ?   
   >   
   > I make a class:   
   >   
   >      template   
   >      class ArrowStarVoidFunc   
   >      {   
   >      private:   
   >          R(O::*fptr)();   
   >      public:   
   >          ArrowStarVoidFunc(R(O::*f)()) : fptr(f) {}   
   >          R operator()(O* o) const { return (o->*fptr)(); }   
   >      };   
   >   
   >      template   
   >      ArrowStarVoidFunc operator->*(R(O::*f)())   
   >          { return ArrowStarVoidFunc(f); }   
   >   
   > Which seems to work fine except it also executes the function.  What   
   > I want to do is the following, but what goes where the ???? is:   
   >   
   >      template   
   >      class ArrowStarVoidFunc   
   >      {   
   >      private:   
   >          R(O::*fptr)();   
   >      public:   
   >          ArrowStarVoidFunc(R(O::*f)()) : fptr(f) {}   
   >          ???? operator()(O* o) const { return &(o->*fptr); }   
   >      };   
   >   
   > Oh, and to make all this more interesting (i.e. complicated) I am   
   > working with a compiler that is more than 10 years old and is only   
   > compatible with C++98 - if I could a newer compiler I wouldn't be   
   > asking this!   
      
   Look at boost::function as a callable with boost::bind for   
   currying. It might still support your 10 years old or more compiler. A   
   boost::function may also save you from having to reinvent the wheel as   
   you are doing with ArrowStarVoidFunc above.   
      
   Let me suggest you give up your 10 year old or more compiler ( I am   
   guessing VC6 ) for something more modern. In a modern compiler you   
   could use std::function and std::bind, both of which came out of   
   boost::function and boost::bind respectively.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca