Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: pass all co...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 16 Topic 9592 of 9823
Post > Topic >>

Re: pass all container elements to member function (for_each and mem_fun_ref)

by Chris Uzdavinis <cuzdav@[EMAIL PROTECTED] > May 13, 2008 at 11:57 AM

On May 12, 11:28 am, Nikola <popiz...@[EMAIL PROTECTED]
> wrote:

[simplified]
> class Simple {};
> class Holder
> {
>      vector<Simple> vec;
>      void Do(const Simple& s) const;
>      void DoAll() {
>          for_each(vec.begin(), vec.end(), mem_fun_ref(&Holder::Do));
>      }
> };
>
> Here is what I get: term does not evaluate to a function taking 1
> arguments

When you pass a mem_fun_ref_t object (created by calling mem_fun_ref),
it creates a funcion object taking objects on which the member
function will be called.  In your case, since you're iterating over
objects of type Simple, the mem_fun_ref should be passed a member
function of Simple, not just any member function pointer will do.

First, on which Holder object would it be invoking this member
function?  When you call for_each as you do, and considering the
arguments given to it, there is nothing that in any way could
associate the "this" pointer of Holder to the function call.

What you need is something more like a Boost or tr1 "bind" object.

  #include "boost/bind.hpp"
  ...

  void DoAll() // member of Holder
  {
    std::for_each(
        vec.begin(),
        vec.end(),
        boost::bind(boost::mem_fn(&Holder::Do), this, _1));
  }

This creates a function object that takes 2 parameters: an object on
which to call the member function, and an argument to pass to the
member function.  That function object is then wrapped using
boost::bind, creating a simpler function object that has argument 1
already bound to "this", the holder object that has the member
function you want to be called.

When used inside for_each, the boost::bind_t object is passed the
current element of the sequence, which is passed to Holder::Do, which
does "whatever Do does".

--
Chris


PS It's also helpful if you include complete code, such as what you
#include, namespace qualifiers, etc.  Then your code can be
cut-and-pasted and we can reproduce the problem by compiling without
having to change your code first to make it "right".


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
pass all container elements to member function (for_each and m
Nikola <popizdeh@[EMAI  2008-05-12 09:28:02 
Re: pass all container elements to member function (for_each an
Francis Glassborow <fr  2008-05-13 10:57:34 
Re: pass all container elements to member function (for_each an
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-13 10:56:39 
Re: pass all container elements to member function (for_each an
Marcin Swiderski <sfid  2008-05-13 10:55:57 
Re: pass all container elements to member function (for_each
Oncaphillis <oncaphill  2008-05-13 10:53:40 
Re: pass all container elements to member function (for_each an
"Jeff Baker" &l  2008-05-13 11:57:27 
Re: pass all container elements to member function (for_each and
Chris Uzdavinis <cuzda  2008-05-13 11:57:28 
Re: pass all container elements to member function (for_each an
d04rp@[EMAIL PROTECTED]   2008-05-13 11:57:28 
Re: pass all container elements to member function (for_each
Oncaphillis <oncaphill  2008-05-13 21:09:43 
Re: pass all container elements to member function (for_each an
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-13 21:07:38 
Re: pass all container elements to member function (for_each
Carl Barron <cbarron41  2008-05-14 08:57:27 
Re: pass all container elements to member function (for_each and
Ulysses4ever@[EMAIL PROTE  2008-05-14 15:12:52 
Re: pass all container elements to member function (for_each and
Krzysztof Czainski <1c  2008-05-14 15:12:02 
Re: pass all container elements to member function (for_each an
Sashi Asokarajan <sash  2008-05-14 17:53:08 
Re: pass all container elements to member function (for_each and
Mateusz Adamczyk <mate  2008-05-14 17:53:27 
Re: pass all container elements to member function (for_each and
Mathias Gaunard <loufo  2008-05-14 17:52:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 1:42:28 CDT 2008.