fork download
  1. #include <iostream>
  2. #include <tr1/functional>
  3.  
  4. class A {
  5. public:
  6. virtual int foo(int p) {
  7. std::cout << "A::foo(" << p << ")" << std::endl;
  8. return 0;
  9. }
  10. };
  11.  
  12. class B : public A {
  13. public:
  14. virtual int foo(int p) {
  15. std::cout << "B::foo(" << p << ")" << std::endl;
  16. return 0;
  17. }
  18. };
  19.  
  20. class C{
  21. };
  22.  
  23. class D : public C, public A{
  24. public:
  25. virtual int foo(int p) {
  26. std::cout << "D::foo(" << p << ")" << std::endl;
  27. return 0;
  28. }
  29. };
  30.  
  31. int foo(int x) {
  32. std::cout << "foo(" << x << ")" << std::endl;
  33. return 0;
  34. }
  35.  
  36. int main() {
  37. using std::tr1::function;
  38. using std::tr1::bind;
  39. using std::tr1::placeholders::_1;
  40.  
  41. A *a = new A;
  42. A *b = new B;
  43. A *d = new D;
  44. function<int(int)> d1 = bind(&A::foo, a, _1);
  45. function<int(int)> d2 = bind(&A::foo, b, _1);
  46. function<int(int)> d3 = bind(&A::foo, d, _1);
  47. function<int(int)> d4 = foo;
  48. d1(100);
  49. d2(100);
  50. d3(100);
  51. d4(100);
  52. }
  53.  
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
A::foo(100)
B::foo(100)
D::foo(100)
foo(100)