Let's go!!!!!
分类: C/C++
2017-02-20 23:25:07
// 麻雀能飞,也能跑
class Sparrow
{
public:
void fly();
void run();
};
// 以 closure 作为接口
typedef boost::function
typedef boost::function
typedef boost::function
// 一个既用到run,也用到fly的客户class
class Foo
{
public:
Foo(FlyCallback flyCb, RunCallback runCb) : flyCb_(flyCb), runCb_(runCb)
{ }
private:
FlyCallback flyCb_;
RunCallback runCb_;
};
// 一个既用到run,也用到swim的客户class
class Bar
{
public:
Bar(SwimCallback swimCb, RunCallback runCb) : swimCb_(swimCb), runCb_(runCb)
{ }
private:
SwimCallback swimCb_;
RunCallback runCb_;
};
int main()
{
Sparrow s;
Penguin p;
// 装配起来,Foo要麻雀,Bar要企鹅。
Foo foo(bind(&Sparrow::fly, &s), bind(&Sparrow::run, &s));
Bar bar(bind(&Penguin::swim, &p), bind(&Penguin::run, &p));
}