CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Declare Member Function that accept Functor

    Hello to all, how to declare a member function that accepts functor of other class ?

    Thanks.
    Thanks for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Declare Member Function that accept Functor

    You err... declare the member function to take an object of that other class as an argument.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Declare Member Function that accept Functor

    The typical way to make a function take an arbitrary functor as opposed to a specific type is to use templates. The downside, of course, is that the definition must be inline.

    An alternative is to use std::function (which is available as boost::function prior to C++1x). This is actually necessary if you intend to store the functor somewhere.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Declare Member Function that accept Functor

    Thanks for the reply.

    The purpose of my program to use functor as argument of function is to achieve callback mechanism. I wonder whether my callback mechanism is correct in the method of attach() and notify().

    Code:
    class subject
    {
    public:
     typedef std::vector<observer*>::iterator vecIte;
    
    public:
     subject();
     virtual ~subject();
     
     // Take functor 
     void attach(observer*);
     void detach(observer*);
     // Take generalize functor 
     // 1. Template function
     // 2. boost::function
     
     void notify();
     
     void setValue(int);
     int getValue();
     
     void calculate();
    
    private:
     
     std::vector<observer*> cont;
     int value;
    };
    
    // ==================
    void subject::attach(observer* anObs)
    {
     vecIte myIte = find(cont.begin(), cont.end(), 
           anObs);
     if (myIte == cont.end())
     {
      cont.push_back(anObs);
     }
    
    }
    // ==================
    void subject::notify()
    {
     myForeach(observer* anObs, cont)
     {
      anObs->operator()(this);
     }
    } 
    // ==================
    int main()
    {
     observer anObs;
     subject sub;
    
     sub.attach(&anObs);
     sub.calculate();
    
     return 0;
    }

    Please comment it.

    What is boost::function ?

    AFAIK, it is a functor wrapper but what is the usage of boost::function ?
    Last edited by Peter_APIIT; May 27th, 2010 at 03:51 AM.
    Thanks for your help.

  5. #5
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Declare Member Function that accept Functor

    I'm not sure that you want a functor here. I have usually seen multiple inheritance as a way of solving this. Create an interface class called EventHandler which provides a pure virtual function called processEvent. All observers must inherit from this interface and register with an event supplier. Of course it is possible that it is the only base class needed for some. The point is that an observer could implement any number of interfaces with multiple inheritance. In your case the Observer class could be the event handler and the call back function name can be whatever you wish. You can use dynamic_cast to ensure that the event handler objects implement the observer interface. I'm not sure what your myForeach is supposed to be. If you have a container of pointers you can use std::for_each and create a functor that calls processEvent on each object since you have a container of observers that provides iterator support.
    Last edited by kempofighter; June 1st, 2010 at 06:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured