fork download
  1. #include <iostream>
  2.  
  3. class CBase
  4. {
  5. public:
  6. virtual void fn() = 0;
  7. };
  8.  
  9. class CX
  10. {
  11. public:
  12. virtual void xx()
  13. {
  14. std::cout << "CX" << std::endl;
  15. };
  16. };
  17.  
  18. class C1 : public CBase, CX
  19. {
  20. public:
  21. virtual void fn()
  22. {
  23. std::cout << "C1" << std::endl;
  24. };
  25. };
  26.  
  27. class C2 : public CX, CBase
  28. {
  29. public:
  30. virtual void fn()
  31. {
  32. std::cout << "C2" << std::endl;
  33. };
  34. };
  35.  
  36. class Hoge
  37. {
  38. public:
  39. C1 member1;
  40. C2 member2;
  41. };
  42.  
  43. int main()
  44. {
  45. {
  46. C1 Hoge::*pm1 = &Hoge::member1;
  47. C2 Hoge::*pm2 = &Hoge::member2;
  48.  
  49. Hoge hoge;
  50. (&hoge->*pm1).fn(); // C1
  51. (&hoge->*pm2).fn(); // C2
  52. }
  53.  
  54. {
  55. CBase Hoge::*pm1 = reinterpret_cast<CBase Hoge::*>(&Hoge::member1);
  56. CBase Hoge::*pm2 = reinterpret_cast<CBase Hoge::*>(&Hoge::member2);
  57.  
  58. Hoge hoge;
  59. (&hoge->*pm1).fn(); // C1
  60. (&hoge->*pm2).fn(); // CX ... oops! called CX::xx
  61. }
  62.  
  63. {
  64. CBase Hoge::*pm1 = reinterpret_cast<CBase Hoge::*>(&Hoge::member1);
  65. CBase Hoge::*pm2 = reinterpret_cast<CBase Hoge::*>(&Hoge::member2);
  66.  
  67. Hoge hoge;
  68. (&hoge->*(reinterpret_cast<C1 Hoge::*>(pm1))).fn(); // C1
  69. (&hoge->*(reinterpret_cast<C2 Hoge::*>(pm2))).fn(); // C2
  70. }
  71.  
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
C1
C2
C1
CX
C1
C2