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));

```
You could create a functor class that wraps your Subscriber and Dispatcher classes, e.g.

```
class MyFunctor {
  public:
    virtual void Execute(const Event& event) = 0;
};

class MySubscriberFunctor : public MyFunctor {
  private:
    Subscriber subscriber_;
  public:
    void Execute(const Event& event) {
      subscriber_.receive(event);    
    }
};

class MyDispatcherFunctor : public MyFunctor {
  private:
    Dispatcher dispatcher_;
  public:
    void Execute(const Event& event) {
      dispatcher_.dispatch(event);    
    }
};

```

Your object list could then store these functor wrappers as a list of MyFunctor instances. This way you can call Execute() on them and let them delegate to the underlying classes. You should really have an operator() instead of Execute() to get a "real" functor, but you get the idea.

Cheers