for_each with member fuunctions

Discussion in 'C++' started by Michael Hull, Apr 20, 2007.

  1. Michael Hull

    Michael Hull Guest

    Hi, I know this really shouldn't be that difficult, bt I can't work it
    out:


    class A
    {
    public:
    /* Class Definition*/
    };


    class B
    {
    list< boost::shared_ptr<A> > aList;
    public:
    void DoA( boost::shared_ptr<A> a ) {/* */ }
    void DoAll()
    {

    list<A>::iterator it;
    for(it = aList.begin();it !=aList.end();it++)
    DoA(*it);
    }
    };


    now i have code similar to this, but I am trying to replace it with a
    for_each line.

    for_each( aList.begin(),aList.end(), ????)

    And this is where I am having a problem, if DoA were a free function,
    it would be no problem, i could use ptr_fun, but I can't work out how
    to associate the 'this' instance with the member function to create a
    functor!
    Many thanks in advance

    Mike
    Michael Hull, Apr 20, 2007
    #1
    1. Advertising

  2. Michael Hull

    Tim Love Guest

    Tim Love, Apr 20, 2007
    #2
    1. Advertising

  3. Michael Hull

    Michael Hull Guest

    On 20 Apr, 15:01, (Tim Love) wrote:
    > Michael Hull <> writes:
    > >Hi, I know this really shouldn't be that difficult, bt I can't work it
    > >out:
    > > ...

    >
    > http://www-h.eng.cam.ac.uk/help/tpl/languages/C /mem_fun.html
    > might help.


    Hi,
    Thanks for the quick response, however that is not quite what I want
    to do, i want to call the method B::DoA, not a member function in A,
    which is my understanding of mem_fun
    Michael Hull, Apr 20, 2007
    #3
  4. Michael Hull wrote:
    > On 20 Apr, 15:01, (Tim Love) wrote:
    >> Michael Hull <> writes:
    >>> Hi, I know this really shouldn't be that difficult, bt I can't work
    >>> it out:
    >>> ...

    >>
    >> http://www-h.eng.cam.ac.uk/help/tpl/languages/C /mem_fun.html
    >> might help.

    >
    > Hi,
    > Thanks for the quick response, however that is not quite what I want
    > to do, i want to call the method B::DoA, not a member function in A,
    > which is my understanding of mem_fun


    Take a look at 'mem_fun1' and 'bind1st'. You will probably need to
    bind the 'this' to 'mem_fun1'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 20, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    545
    PJP of NYC
    May 24, 2005
  2. glen stark
    Replies:
    5
    Views:
    506
    glen stark
    Aug 16, 2005
  3. waitan
    Replies:
    2
    Views:
    522
  4. Belebele
    Replies:
    6
    Views:
    338
    Pierre Barbier de Reuille
    Aug 14, 2006
  5. Kenneth Porter
    Replies:
    8
    Views:
    408
    Kai-Uwe Bux
    Oct 14, 2007
Loading...

Share This Page