fork download
  1. #include <iostream>
  2.  
  3. template <class Dest, class Src>
  4. Dest force_cast(Src src)
  5. {
  6. union
  7. {
  8. Dest d;
  9. Src s;
  10. } convertor;
  11. convertor.s = src;
  12. return convertor.d;
  13. }
  14.  
  15. class ABase {
  16. virtual void bind() = 0;
  17. };
  18.  
  19. class B;
  20.  
  21. class A
  22. : public ABase {
  23. public:
  24. // int a, b;
  25.  
  26. class Unknown{};
  27. typedef int(A::*Function)();
  28.  
  29. void bindFunction(void* f) {
  30. this->f = force_cast<Function>(f);
  31. }
  32.  
  33. int call() {
  34. return (this->*f)();
  35. }
  36.  
  37. Function f;
  38. };
  39.  
  40. class B {
  41. private:
  42. int answer;
  43. public:
  44. B(int value) : answer(value) {}
  45.  
  46. // int c, d;
  47. int getValue() { return answer; }
  48. };
  49.  
  50. class Combined
  51. : public A, public B {
  52. public:
  53. Combined(int value) : B(value), A() {}
  54.  
  55. virtual void /*A::*/bind() /* override */ {
  56. A::bindFunction( force_cast<void*>( &Combined::getValue ) );
  57. }
  58. };
  59.  
  60. int main() {
  61. Combined c(42);
  62. c.bind();
  63. std::cout << "The answer to Life, The Universe and Everything is " << c.call() << "\n";
  64. return 0;
  65. }
Success #stdin #stdout 0.01s 16064KB
stdin
Standard input is empty
stdout
The answer to Life, The Universe and Everything is 1991543360