私はリストを持っており、Thing
それぞれのController
ことをやりたいと思ってnotify()
います。以下のコードは動作します:
#include <algorithm>
#include <iostream>
#include <tr1/functional>
#include <list>
using namespace std;
class Thing { public: int x; };
class Controller
{
public:
void notify(Thing& t) { cerr << t.x << endl; }
};
class Notifier
{
public:
Notifier(Controller* c) { _c = c; }
void operator()(Thing& t) { _c->notify(t); }
private:
Controller* _c;
};
int main()
{
list<Thing> things;
Controller c;
// ... add some things ...
Thing t;
t.x = 1; things.push_back(t);
t.x = 2; things.push_back(t);
t.x = 3; things.push_back(t);
// This doesn't work:
//for_each(things.begin(), things.end(),
// tr1::mem_fn(&Controller::notify));
for_each(things.begin(), things.end(), Notifier(&c));
return 0;
}
Notifier
私の質問は、「これは機能しません」行のいくつかのバージョンを使用してクラスを取り除くことはできますか? 何かを機能させることができるはずですが、適切な組み合わせを得ることができません。(いろいろ組み合わせを考えてみました。)
ブーストを使わずに?(できればそうします。)私はg ++ 4.1.2を使用しています。はい、古いことは知っています...