Question: I have the following two classes that exhibit the Command pattern. (Unfortunately, both have a unique method name.)

```
//pseudocode 
class Subscriber {
    virtual void receive(const Event&) = 0;
}

class Dispatcher {
    virtual void dispatch(const Event&) = 0;
}

```

I have a class template that has a list of some type with a method to iterate over this list.

```
//pseudocode
template<typename T>
class Registry {
    typedef list<T> ObjectList;
    ObjectList _objects;
    void iterate(const Event& event) {
        for_each(_objects.begin(), _objects.end(), ...);  //not sure what to do here
    }
}

```

I would like to use `mem_fun` to create a Functor that calls `receive` or `dispatch` as appropriate. I'm able to create a simple use case where I simply invoke a method without any params. I.e.

```
//pseudocode
class Simple {
    void simple() {/*...*/}
}

```

and then I iterate:

```
for_each(_objects.begin(), _objects.end(), mem_fun(&Simple::simple);

```

Unfortunately, I have no idea how to get the `event` param passed to mem\_fun. Looking at the headers, it does appear that I can pass a single param, but I'm not well versed in C++ to understand what I need to do.

Ultimately, I would like to make the `iterate` method accept a type of functor so it will fire that method on every method in the list.

I would prefer to avoid Boost...I think this is entirely possible without dragging this framework into the mix.

Thanks!

Answer: 
This may be the most straight-forward way:

```
class event_forwarder // make private in Registry
{
public:
    event_forwarder(const Event& event) :
    mEvent(event)
    {}

    void operator()(Subscriber& subcriber) const
    {
        subscriber.receive(mEvent);
    }

    void operator()(Dispatcher& dispatcher) const
    {
        dispatcher.dispatch(mEvent);
    }

private:
    const Event& mEvent;
};

```

Then:

```
for_each(_objects.begin(), _objects.end(), event_forwarder(event));

```
Check if you have tr1. If you have tr1, it contains `std::bind`, which is almost exactly equivalent to the `boost` implementation. This should be found in the `<functional>` header.

If you don't have tr1, consider using Boost. I would strongly suggest using at least `boost::bind`, as it's lightweight and header only.

If you don't have tr1 and won't use Boost, you want to mix `bind2nd` and `mem_fun1`. The first binds the second parameter (in this case, your event; the object will be the first) and `mem_fun1` is the same as `mem_fun`, but it expects two arguments, the object to be called on and one parameter to pass the member function being called. This is a complete mess, though.

If you do have access to `bind`, it's fairly easy.

`for_each(objects.begin(), objects.end(), bind(&Simple::simple, _1, event))`