By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Using an object's member function as the action passed to for_each

P: n/a
Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
....
for_each(0, 5, /*i want to call a.foo for each element in the range*/);


What is a good elegant way to do that?

What I did was to wrap the a object into a unary_function-derived
object and then pass the unary_function to the for_each method.

Any other ideas?

Thanks

Aug 14 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Belebele schrieb:
Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the range*/);

What is a good elegant way to do that?
#include <algorithm>
#include <boost/bind.hpp>

class A {
public:
void foo(int i) { };
};

int main()
{
A a;

int ints[5] = { 1, 2, 3, 4, 5};

std::for_each(ints, ints+5, boost::bind(&A::foo, a, _1));
}

There may be a way using std::bind* functions.

--
Thomas
Aug 14 '06 #2

P: n/a
Belebele wrote:
Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the range*/);


What is a good elegant way to do that?

What I did was to wrap the a object into a unary_function-derived
object and then pass the unary_function to the for_each method.

Any other ideas?

Thanks
The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));

Another option is to use the boost::bind library, in which case it would
look like:

for_each(0, 5, bind(A::foo, a, _1));

Pierre
Aug 14 '06 #3

P: n/a
Thomas J. Gritzan wrote:
Belebele schrieb:
>Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the
range*/);

What is a good elegant way to do that?

#include <algorithm>
#include <boost/bind.hpp>

class A {
public:
void foo(int i) { };
};

int main()
{
A a;

int ints[5] = { 1, 2, 3, 4, 5};

std::for_each(ints, ints+5, boost::bind(&A::foo, a, _1));
}

There may be a way using std::bind* functions.
std::for_each(ints, ints+5,
std::bind1st(std::mem_fun1(&A::foo), &a));

(include <functionalinstead of <boost..>)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 14 '06 #4

P: n/a
Pierre Barbier de Reuille wrote:
[..]
The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));
Try it before recommending it. It won't work. Binders are tricky.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 14 '06 #5

P: n/a
class A {
...
public:
void foo(int );
};

The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));
So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.

Thanks!

Aug 14 '06 #6

P: n/a
Belebele wrote:
>class A {
...
public:
void foo(int );
};

The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));

So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.

Thanks!
bind1st doesn't do anything like that. It just binds the first argument
of the function. It is std::mem_fun that convert a member function into
an object whose first argument is the object needed for the member
function. Also, as suggested higher, the for_each won't work like that,
you need an iterator (sorry, I copied that a bit quickly), so it should be:

int ints[6] = {0,1,2,3,4,5};
for_each(ints, ints+6, std::bind1st(std::mem_fun(&A::foo), &a));

(I also forgot the references ...)

Pierre
Aug 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.