The easiest with TR1 / C ++ 11:
#include <vector>
#include <functional>
#include <algorithm>
struct Object2{};
struct Object1 {
void foo(Object2*) {}
};
int main() {
std::vector<Object1> vec;
Object2 obj2;
std::for_each(vec.begin(), vec.end(), std::bind(&Object1::foo, std::placeholders::_1, &obj2));
}
But you can also use std::for_eachwith std::bind2ndand std::mem_fun_ref, if this is not an option:
std::for_each(vec.begin(), vec.end(), std::bind2nd(std::mem_fun_ref(&Object1::foo), &obj2));
Flexo source
share