Guest User

Untitled

a guest
Dec 9th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. class A {
  2. std::vector<Method> Methods;
  3. void DoSomething(int methodNo, Arg1* arg1, Arg2* arg2) {
  4. (this->*(Methods[methodNo]))(arg1, arg2);
  5. }
  6. }
  7.  
  8. class B : public A{
  9. B() {
  10. Methods.push_back(&B::Something);
  11. }
  12.  
  13. Something(Arg1* arg1, Arg2* arg2) { }
  14. }
  15.  
  16. struct Base {};
  17.  
  18. struct Derived : Base {
  19. void foo() { std::cout << "Hello World!" << std::endl; }
  20. };
  21.  
  22. struct DerivedDerived : Derived {
  23. void foo() { std::cout << "Um..." << std::endl; }
  24. };
  25.  
  26. struct Derived2 : Base {
  27. void foo() { std::cout << "Goodbye World!" << std::endl; }
  28. };
  29.  
  30. struct Independent {
  31. void foo() { std::cout << "Huh?" << std::endl; }
  32. };
  33.  
  34. int main()
  35. {
  36. Derived d;
  37. DerivedDerived dd;
  38. Derived2 d2;
  39.  
  40. void (Base::*pfoo)() = static_cast<void (Base::*)()>(&Derived::foo);
  41. // Корректное преобразование, поддерживается `static_cast`
  42.  
  43. Base *b = &d;
  44. (b->*pfoo)();
  45. // Корректное выражение, корректный вызов `d.foo()`
  46.  
  47. (d.*pfoo)();
  48. // Корректное выражение, корректный вызов `d.foo()`
  49.  
  50. b = &dd;
  51. (b->*pfoo)();
  52. // Корректное выражение, корректный вызов `dd.Derived::foo()`
  53.  
  54. (dd.*pfoo)();
  55. // Корректное выражение, корректный вызов `dd.Derived::foo()`
  56.  
  57. b = &d2;
  58. (b->*pfoo)();
  59. // Корректное выражение, некорректный вызов - поведение не определено
  60.  
  61. (d2.*pfoo)();
  62. // Корректное выражение, некорректный вызов - поведение не определено
  63.  
  64. Independent i;
  65. (i.*pfoo)();
  66. // Некорректное выражение, ошибка компиляции
  67.  
  68. pfoo = static_cast<void (Base::*)()>(&Independent::foo);
  69. // Некорректное преобразование, не поддерживается `static_cast`,
  70. // ошибка компиляции
  71. }
  72.  
  73. Methods.push_back(static_cast<Method>(&B::Something));
  74.  
  75. template <typename T> add_method(T method)
  76. {
  77. Methods.push_back(static_cast<Method>(method));
  78. }
  79.  
  80. class B;
  81.  
  82. class A
  83. {
  84. using B_Method = void ( B::* )(Arg1 * arg1, Arg2 * arg2);
  85. std::vector<B_Method> m_methods;
  86. void DoSomething(int methodNo, Arg1 * arg1, Arg2 * arg2);
  87. };
  88.  
  89. class B : public A
  90. {
  91. B(void)
  92. {
  93. Methods.push_back(&B::Something);
  94. }
  95.  
  96. void Something(Arg1* arg1, Arg2* arg2) { }
  97. };
  98.  
  99. void A::DoSomething(int methodNo, Arg1 * arg1, Arg2 * arg2)
  100. {
  101. (static_cast<B *>(this)->*(m_methods[methodNo]))(arg1, arg2);
  102. }
  103.  
  104. class B;
  105.  
  106. typedef void(B::*b_method)(Arg1 *, Arg2 *);
  107.  
  108. class A
  109. {
  110. std::vector<b_method> Methods;
  111. void DoSomething(int methodNo, Arg1* arg1, Arg2* arg2);
  112. }
  113.  
  114. void B::DoSomething(int methodNo, Arg1* arg1, Arg2* arg2)
  115. {
  116. if((B *selfPtr = dynamic_cast<B *>(this))) // check for inherit
  117. {
  118. (selfPtr->*(m_methods[methodNo]))(arg1, arg2);
  119. }
  120. }
  121.  
  122. class B : public A
  123. {
  124. B()
  125. {
  126. Methods.push_back(&B::Something);
  127. }
  128.  
  129. Something(Arg1* arg1, Arg2* arg2) { }
  130. }
Add Comment
Please, Sign In to add comment