fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class base
  6. {
  7. protected:
  8. int x; /*position*/
  9. public:
  10. base();
  11. int getX() const; /*get position*/
  12. void setX(int); /*set position*/
  13. friend ostream &operator<<(ostream &, base);
  14. };
  15.  
  16. class A: public base
  17. {
  18. /*memebers and memeber functions*/
  19. };
  20.  
  21. class B: public base
  22. {
  23. private:
  24. int (A::*fp_getA_X)() const = &A::getX; /*pointer to memeber function */
  25. A const * target = nullptr;
  26. public:
  27. B(const A*);
  28. void setA(const A*);
  29. int getA_X() const;
  30. friend ostream &operator<<(ostream &, B);
  31. };
  32.  
  33. B::B(const A* a): target(a){};
  34.  
  35. void B::setA(const A* a){ target = a; }
  36.  
  37. int B::getA_X() const
  38. {
  39. return target->getX();
  40. /* return (target->*fp_getA_X)(); // alternative */
  41. }
  42. ostream &operator<<(ostream &os, B b)
  43. {
  44. os << "a's position (" << b.getA_X() << ")";
  45. return os;
  46. }
  47.  
  48. base::base():x(0){}
  49. int base::getX() const { return x; }
  50. void base::setX(int n) { x = n; }
  51. ostream &operator<<(ostream &os, base obj)
  52. {
  53. os << "(" << obj.getX() << ")";
  54. return os;
  55. }
  56.  
  57. int main()
  58. {
  59. // #: 可以代表任一數字
  60. /*
  61.   b#們全部一開始得到a1的參考,
  62.   a1.x改變b#們都可以知道,
  63.   而b#也有自己的x (用static_cast<base>(b1)或b1.getX()查看)。
  64.   最後b3變成參考a2,
  65.   a2.x改變b3就可以知道。
  66.   */
  67. A a1, a2;
  68. B b1(&a1),b2(&a1),b3(&a1);
  69. cout << "a1 = " << a1 << endl;
  70. cout << "b1 : " << b1 << endl;
  71. cout << "b1 = " << "(" << b1.getX() << ")" << endl;
  72. cout << "b1 = " << (static_cast<base>(b1)) << endl;
  73. cout << endl;
  74.  
  75. a1.setX(2);
  76. b1.setX(4);
  77. b2.setX(5);
  78. b3.setX(6);
  79. cout << "a1 = " << a1 << endl;
  80. cout << "b1 : " << b1 << endl;
  81. cout << "b1 = " << (static_cast<base>(b1)) << endl;
  82. cout << "b2 = " << (static_cast<base>(b2)) << endl;
  83. cout << "b3 = " << (static_cast<base>(b3)) << endl;
  84. cout << endl;
  85.  
  86. a2.setX(22);
  87. b3.setA(&a2);
  88. cout << "a1 = " << a1 << endl;
  89. cout << "a2 = " << a2 << endl;
  90. cout << "b1 : " << b1 << endl;
  91. cout << "b1 = " << (static_cast<base>(b1)) << endl;
  92. cout << "b2 = " << (static_cast<base>(b2)) << endl;
  93. cout << "b3 = " << (static_cast<base>(b3)) << endl;
  94. cout << "b3 : " << b3 << endl;
  95. cout << endl;
  96.  
  97. a2.setX(34);
  98. b3.setX(-2);
  99. cout << "a2 = " << a2 << endl;
  100. cout << "b3 : " << b3 << endl;
  101. cout << "b3 = " << (static_cast<base>(b3)) << endl;
  102.  
  103. return 0;
  104. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
a1 = (0)
b1 : a's position (0)
b1 = (0)
b1 = (0)

a1 = (2)
b1 : a's position (2)
b1 = (4)
b2 = (5)
b3 = (6)

a1 = (2)
a2 = (22)
b1 : a's position (2)
b1 = (4)
b2 = (5)
b3 = (6)
b3 : a's position (22)

a2 = (34)
b3 : a's position (34)
b3 = (-2)