473,799 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
6 4036
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(i nts, ints+5, boost::bind(&A: :foo, a, _1));
}

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

--
Thomas
Aug 14 '06 #2
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(st d::mem_fun(A::f oo), 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
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(i nts, ints+5, boost::bind(&A: :foo, a, _1));
}

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

(include <functionalinst ead 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
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(st d::mem_fun(A::f oo), 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
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(st d::mem_fun(A::f oo), a));
So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.

Thanks!

Aug 14 '06 #6
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(st d::mem_fun(A::f oo), 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(st d::mem_fun(&A:: foo), &a));

(I also forgot the references ...)

Pierre
Aug 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1937
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the board. Since this function does not change anything in the class I declared it as const. To count all pieces of a given color the functions iterates through a "map" of CNode-pointers. "CNode" is another class that is irrelevant to the problem.
1
7542
by: flyaflya | last post by:
i use a class static member function to for_each: class t1 { static test(string s); } main() { list<string> slist; ...
8
1440
by: pookiebearbottom | last post by:
Ok, this is what I am trying to do. I have a member function that as a parameter accepts a pointer to a different member function. In this 2nd function, I want to iterate over an STL container and call the passed in member function. I can do this in my own loop, but I am looking to call for_each. I am a little confused on the syntax to use. Any help would be appreciated. look at the following code and at the comment in
11
6604
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
3
2803
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the iteration, which forces me to have certain interface on the Iterator class. struct Element { virtual ~Element() = 0; };
13
4419
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do the same.
3
1575
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The presence of this member variable prevents me from using a static or global method to be passed as a third parameter to for_each, and mem_fun doesn't seem to work for me either as I am not going to execute a method of an iterator but pass an iterator...
9
2104
by: want.to.be.professer | last post by:
We know that alomost every algorithm function, such as for_each, find_if, use funcional as well as function pointer. But when I want to use another class's member function, how could I do? See example: class TestPrint { public:
2
4906
by: church7 | last post by:
Hello, I wrote the following class: In this case, the error message was
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.