請考慮以下示例。boost :: bind和類成員函數
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/bind.hpp>
void
func(int e, int x) {
std::cerr << "x is " << x << std::endl;
std::cerr << "e is " << e << std::endl;
}
struct foo {
std::vector<int> v;
void calc(int x) {
std::for_each(v.begin(), v.end(),
boost::bind(func, _1, x));
}
void func2(int e, int x) {
std::cerr << "x is " << x << std::endl;
std::cerr << "e is " << e << std::endl;
}
};
int
main()
{
foo f;
f.v.push_back(1);
f.v.push_back(2);
f.v.push_back(3);
f.v.push_back(4);
f.calc(1);
return 0;
}
所有,如果我使用func()
功能工作正常。但在現實生活中,我必須使用類成員函數,例如foo::func2()
。我如何用boost :: bind做到這一點?
這應該與boost :: lamba :: bind一起使用。我沒有使用boost :: bind很多。 – 2009-06-15 10:17:05