for_each with a non const member fuction

G

glen stark

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
 
R

ravinderthakur

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::eek:perator()(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());
}
 
J

Jeff Flinn

glen stark said:
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.

Use the boost bind library:
So, I want to use a std::for_each in the following situation:

#include said:
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() )

std::for_each( bPtrVec.begin()
, bPtrVec.end()
, boost::bind( &A::member_function, this, _1 )
);

Jeff Flinn
 
E

Earl Purple

glen said:
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.

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).
 
G

glen stark

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.

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.
 
G

glen stark

Use the boost bind library:
std::for_each( bPtrVec.begin()
, bPtrVec.end()
, boost::bind( &A::member_function, this, _1 )
);


Jeff Flinn
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top