#include <iostream>
#include <tr1/functional>
class A {
public:
virtual int foo(int p) {
std::cout << "A::foo(" << p << ")" << std::endl;
return 0;
}
};
class B : public A {
public:
virtual int foo(int p) {
std::cout << "B::foo(" << p << ")" << std::endl;
return 0;
}
};
class C{
};
class D : public C, public A{
public:
virtual int foo(int p) {
std::cout << "D::foo(" << p << ")" << std::endl;
return 0;
}
};
int foo(int x) {
std::cout << "foo(" << x << ")" << std::endl;
return 0;
}
int main() {
using std::tr1::function;
using std::tr1::bind;
using std::tr1::placeholders::_1;
A *a = new A;
A *b = new B;
A *d = new D;
function<int(int)> d1 = bind(&A::foo, a, _1);
function<int(int)> d2 = bind(&A::foo, b, _1);
function<int(int)> d3 = bind(&A::foo, d, _1);
function<int(int)> d4 = foo;
d1(100);
d2(100);
d3(100);
d4(100);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHIxL2Z1bmN0aW9uYWw+CgpjbGFzcyBBIHsKcHVibGljOgogICAgdmlydHVhbCBpbnQgZm9vKGludCBwKSB7CiAgICAgICAgc3RkOjpjb3V0IDw8ICJBOjpmb28oIiA8PCBwIDw8ICIpIiA8PCBzdGQ6OmVuZGw7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9Cn07CgpjbGFzcyBCIDogcHVibGljIEEgewpwdWJsaWM6CiAgICB2aXJ0dWFsIGludCBmb28oaW50IHApIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgIkI6OmZvbygiIDw8IHAgPDwgIikiIDw8IHN0ZDo6ZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KfTsKCmNsYXNzIEN7Cn07CgpjbGFzcyBEIDogcHVibGljIEMsIHB1YmxpYyBBewpwdWJsaWM6CiAgICB2aXJ0dWFsIGludCBmb28oaW50IHApIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgIkQ6OmZvbygiIDw8IHAgPDwgIikiIDw8IHN0ZDo6ZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KfTsKCm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