std::function<>
C++11 が利用できない場合の代用として、どの構造を使用する必要がありますか?
代替手段は、基本的に、以下の例のように、あるクラスのプライベート メンバー関数に別のクラスからアクセスできるようにする必要があります (std::function の他の機能は使用されません)。クラス Foo は固定されており、あまり変更できません。クラス Bar にしかアクセスできません。
class Foo {
friend class Bar; // added by me, rest of the class is fixed
private:
void doStuffFooA(int i);
void doStuffFooB(int i);
};
class Bar {
public:
Bar( Foo foo, std::function< void (const Foo&, int) > func ) {
myFoo = foo;
myFooFunc = func;
};
private:
doStuffBar( const &Foo foo ) {
myFooFunc( foo, 3 );
}
Foo myFoo;
std::function< void (const Foo&, int) > myFooFunc;
}
int main() {
Foo foo(...);
Bar barA( foo, &Foo::doStuffFooA );
Bar barB( foo, &Foo::doStuffFooB );
...
}