Rozważmy następujący przykład.Funkcja zwiększenia :: powiązanie i klasa członkowska
#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;
}
Wszystko działa dobrze, jeśli mogę użyć func()
funkcję. Ale w prawdziwym życiu muszę użyć w tym przykładzie funkcji klasy członkowskiej, tj. foo::func2()
. Jak mogę to zrobić za pomocą metody boost :: bind?
To ma działać z boost :: lamba :: bind. Nie użyłem boost :: bind dużo. –