fork download
  1. #include <iostream>
  2.  
  3. typedef void(*ISSFUNC)(void*);
  4.  
  5. struct AInst
  6. {
  7. void* data;
  8. ISSFUNC func;
  9. };
  10.  
  11. AInst* mInst;
  12.  
  13. void saveAInst(void* inst, ISSFUNC func)
  14. {
  15. mInst = new AInst();
  16. mInst->data = inst;
  17. mInst->func = func;
  18. }
  19.  
  20. void issueInst()
  21. {
  22. mInst->func(mInst->data);
  23. delete mInst;
  24. }
  25.  
  26. class AHandler
  27. {
  28. public:
  29. virtual void print() = 0;
  30. };
  31.  
  32. class A
  33. {
  34. private:
  35. AHandler* mHandler;
  36.  
  37. static void funct(void* data)
  38. {
  39. A* a = reinterpret_cast<A*>(data);
  40. a->mHandler->print();
  41. }
  42. public:
  43. A(AHandler* handler = nullptr) : mHandler(handler)
  44. {
  45. saveAInst(this, &funct);
  46. }
  47. };
  48.  
  49. class AHandlerImpl1 : public AHandler
  50. {
  51. public:
  52. virtual void print() override
  53. {
  54. std::cout << "Implementation 1\n";
  55. }
  56. };
  57.  
  58. class AHandlerImpl2 : public AHandler
  59. {
  60. public:
  61. virtual void print() override
  62. {
  63. std::cout << "Implementation 2\n";
  64. }
  65. };
  66.  
  67. int main()
  68. {
  69. AHandlerImpl1 impl1;
  70. AHandlerImpl2 impl2;
  71.  
  72. A a1(&impl1);
  73.  
  74. issueInst();
  75.  
  76. A a2(&impl2);
  77.  
  78. issueInst();
  79.  
  80. return 0;
  81. }
  82.  
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
Implementation 1
Implementation 2