Boost logo

Boost Users :

Subject: Re: [Boost-users] questions regarding boost::mem_fn and boost::bind
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-04-02 19:50:08


AMDG

On 04/02/2011 04:15 PM, Littlefield, Tyler wrote:
> Hello all:
> I have a quick question. I was trying to learn how to use boost::mem_fn
> to work as a sort of function object, because I need something like this
> for my library.
> So: If I understand this correctly, it creates a function object that
> has () overloaded, correct?
> I was curious if someone could then help me figure this out, the
> documentation is confusing for me.
> lets say I have this class:
> class Foo
> {
> public:
> int Add(int a, int b)
> {
> return (a+b);
> }
> };
>
> and I could do Foo bar;
> how would I create a mem_fn named f, that would let me call it for add?
> so: f(3,1).
> I would have to tie the instance of foo (bar) to this somehow, correct?

Yes. That's what bind does.

> Next, I see that mem_fn creates a function object which basically does
> what I want. What then is the point in boost::bind? It looks to do about
> the same as mem_fn, just makes my head hurt a little bit more.
>

mem_fn doesn't do any argument binding.
It only adapts a member function to be
a regular function object. boost::mem_fn(&Foo::Add)
takes 3 arguments.

> I am trying to learn boost by finding uses for some of the libraries.
> Some of them sort of makes sense, but I see that mem_fn are the bases
> for a lot, so I wanted to learn how to use those. After bind and mem_fn,
> what other libraries would it be best to learn that is used all over?
> Last, this is the use I will be putting mem_fn to. If this is incorrect,
> I still want to learn, but it looked like a great example.
> I am writing a quick socket class that will have connect, close, and
> send capabilities. I am hooking this to my game engine, and wanted to
> use a mem_fn to receive the data. So the server polls all user input,
> then it will poll the sockets that were added to another list. It will
> pass off any data read by calling the function object with the data as
> an argument, as well as the socket.

That sounds rather like what Boost.Asio does.

> Sorry for all the questions, and thanks in advance for the
> help/clarification.
>

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net