
August 16th, 2005, 12:05 PM
| | | for_each with a non const member fuction
Hi Everyone
So, I want to use a std::for_each in the following situation:
class B{
//whatever
}
class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
void member_function(B*); //non const.
}
A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )
}
i.e. I want to use std::for_each to call a member (of A) function,
with a single parameter, the pointer pointed to by the iterator I am
for_eaching over.
Unfortunatley I'm tripped up by the whole binding thing... can someone help
me out? I'm also willing to use the boost lambda libraries, but I haven't
been
able to sort this out.
Thanks,
Glen | 
August 16th, 2005, 12:35 PM
| | | Re: for_each with a non const member fuction
one way to use the for_each you need to create a function object. This
can be done by overriding the operator() of the class.
delare the class A as
class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
//void member_function(B*); //non const.
void operator()(const B* value);
}
A::operator()(const B* value)
{
//do what ever you want to do with B*
}
The function A::configure_A_from_B will be modified as :
A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), A());
} | 
August 16th, 2005, 01:15 PM
| | | Re: for_each with a non const member fuction
"glen stark" <stark@ife.ee.ethz.ch> wrote in message
news:4301c5e6$1@news1.ethz.ch...[color=blue]
> Hi Everyone
>
> i.e. I want to use std::for_each to call a member (of A) function,
> with a single parameter, the pointer pointed to by the iterator I am
> for_eaching over.
>
> Unfortunatley I'm tripped up by the whole binding thing... can someone
> help
> me out? I'm also willing to use the boost lambda libraries, but I haven't
> been
> able to sort this out.[/color]
Use the boost bind library:
[color=blue]
>
> So, I want to use a std::for_each in the following situation:[/color]
#include <boost/bind.hpp>
[color=blue]
> class B{
> //whatever
> }
>
> class A{
> private:
> void configure_A_from_B(const std::vector<B*>& bPtrVec);
> void member_function(B*); //non const.
> }
>
> A::configure_A_from_B(const std::vector<B*>& bPtrVec){
> std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )[/color]
std::for_each( bPtrVec.begin()
, bPtrVec.end()
, boost::bind( &A::member_function, this, _1 )
);
[color=blue]
> }[/color]
Jeff Flinn | 
August 16th, 2005, 01:25 PM
| | | Re: for_each with a non const member fuction
glen stark wrote:[color=blue]
> Hi Everyone
>
> So, I want to use a std::for_each in the following situation:
>
> class B{
> //whatever
> }
>
> class A{
> private:
> void configure_A_from_B(const std::vector<B*>& bPtrVec);
> void member_function(B*); //non const.
> }
>
> A::configure_A_from_B(const std::vector<B*>& bPtrVec){
> std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )
> }
>
> i.e. I want to use std::for_each to call a member (of A) function,
> with a single parameter, the pointer pointed to by the iterator I am
> for_eaching over.
>
> Unfortunatley I'm tripped up by the whole binding thing... can someone help
> me out? I'm also willing to use the boost lambda libraries, but I haven't
> been
> able to sort this out.[/color]
Do you mean boost::bind?
std::bind1st and std::mem_fun combination do not allow a non-const
member function so you'd have to try either boost::bind or write your
own function object.
boost::bind would work (I think) thus:
std::for_each( bPtrVec.begin(), bPtrVec.end(),
boost::bind( &A::member_function, this, _1 ) );
They're not very clear with their examples for this typical situation
(I don't know why). | 
August 16th, 2005, 01:25 PM
| | | Re: for_each with a non const member fuction
<ravinderthakur@gmail.com> wrote in message
news:1124191574.066590.231440@g14g2000cwa.googlegr oups.com...[color=blue]
> one way to use the for_each you need to create a function object. This
> can be done by overriding the operator() of the class.
>[/color]
Thanks for the suggestion.I forgot to mention, I don't want to create an
explicit function object. I run into this situation all the time, and it
seems to me that there must be a way to bind the member function (perhaps
using boost lambda libraries
to be able to do this in a quick clean way without having to create a
function object for every instance I want to do this. | 
August 16th, 2005, 01:45 PM
| | | Re: for_each with a non const member fuction
> Use the boost bind library:[color=blue]
>[color=green]
>>
>> So, I want to use a std::for_each in the following situation:[/color]
>
> #include <boost/bind.hpp>
>[color=green]
>> class B{
>> //whatever
>> }
>>
>> class A{
>> private:
>> void configure_A_from_B(const std::vector<B*>& bPtrVec);
>> void member_function(B*); //non const.
>> }
>>
>> A::configure_A_from_B(const std::vector<B*>& bPtrVec){
>> std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )[/color]
>
> std::for_each( bPtrVec.begin()
> , bPtrVec.end()
> , boost::bind( &A::member_function, this, _1 )
> );
>[color=green]
>> }[/color]
>
> Jeff Flinn
>[/color]
Thanks, that's just what I was looking for. I tried the bind library, but
didn't get the syntax right for
the member function (didn't have the 'this' argument).
glen |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|