views:

184

answers:

1

I have

class A: public B { ...}

vector<A*> v;

I want to do

for_each(v.begin(), v.end(), mem_fun_deref(B::blah()));

(Actually I have:

vector<unique_ptr<A>>

but it shouldn't matter)

I need to upcast and call the member function.

+2  A: 

boost::lambda can do it

vector<A*> v; ...
using boost::lambda::_1;
using boost::lambda::bind;
for_each(v.begin(), v.end(), bind(&B::blah, *_1));

No need to upcast. A member pointer to a base-class member can be applied to a derived class too.

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
Johannes Schaub - litb
thanks! .
Neil G