Chinaunix首页 | 论坛 | 博客
  • 博客访问: 774778
  • 博文数量: 239
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1045
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-22 18:25
文章分类

全部博文(239)

文章存档

2019年(9)

2018年(64)

2017年(2)

2016年(26)

2015年(30)

2014年(41)

2013年(65)

2012年(2)

分类: C/C++

2012-06-26 10:37:44

C++的private成员函数限制被外部调用, 但如果想调用子类的private成员函数, 除了使用friend方法, 以下是另外一种方法: 使用多态的继承体系结构.

点击(此处)折叠或打开

  1. #include <iostream>
  2. using namespace std;
  3. #define INHERIT_SUB
  4. #ifdef INHERIT_SUB
  5. class Superclass
  6. {
  7. public:
  8.  void test()
  9.  {
  10.   int delay = 0;
  11.   delay = delay;
  12.   PolymorphismFun();
  13.   this->PolymorphismFun();
  14.  }
  15. protected:
  16.  virtual void PolymorphismFun()
  17.  {
  18.   int delay = 0;
  19.   delay = delay;
  20. #pragma message("Superclass::PolymorphismFun")
  21.   cout<<"Superclass::PolymorphismFun"<<endl;
  22.  }
  23.  virtual ~Superclass()
  24.  {
  25.   int delay = 0;
  26.   delay = delay;
  27.  }
  28. };
  29. class SubclassA : public Superclass
  30. {
  31. public:
  32.  void test()
  33.  {
  34.   int delay = 0;
  35.   delay = delay;
  36.   PolymorphismFun();
  37.   this->PolymorphismFun();
  38.  }

  39.  void PolymorphismFun()
  40.  {
  41.   int delay = 0;
  42.   delay = delay;
  43. #pragma message("SubclassA::PolymorphismFun")
  44.   cout<<"SubclassA::PolymorphismFun"<<endl;
  45.  }

  46.  virtual ~SubclassA()
  47.  {
  48.   int delay = 0;
  49.   delay = delay;
  50.  }
  51. };
  52. class SubclassB : public SubclassA
  53. {
  54. public:
  55.  void test()
  56.  {
  57.   int delay = 0;
  58.   delay = delay;
  59.   PolymorphismFun();
  60.   this->PolymorphismFun();
  61.  }
  62. private:
  63.  virtual void PolymorphismFun()=0;
  64. public:
  65.  virtual void operator()()
  66.  {
  67.   int delay = 0;
  68.   delay = delay;
  69.  };
  70.  virtual ~SubclassB()
  71.  {
  72.   int delay = 0;
  73.   delay = delay;
  74.  }
  75. };

  76. class SubclassC : public SubclassB
  77. {
  78. public:
  79.  void test()
  80.  {
  81.   int delay = 0;
  82.   delay = delay;
  83.   PolymorphismFun();
  84.   this->PolymorphismFun();
  85.  }
  86. private:
  87.  void PolymorphismFun()
  88.  {
  89.   int delay = 0;
  90.   delay = delay;
  91. #pragma message("SubclassC::PolymorphismFun")
  92.   cout<<"SubclassC::PolymorphismFun"<<endl;
  93.  }
  94. public:
  95.  virtual void operator()()
  96.  {
  97.   int delay = 0;
  98.   delay = delay;
  99.  };
  100.  virtual ~SubclassC()
  101.  {
  102.   int delay = 0;
  103.   delay = delay;
  104.  }
  105. };
  106. #endif//INHERIT_SUB

  107. int main(int argc, char *argv[])
  108. {
  109. #pragma message("main starting")
  110. #ifdef INHERIT_SUB
  111.  SubclassC testSubclassC;
  112.  testSubclassC.SubclassB::test();
  113.  int nSize = sizeof(testSubclassC);
  114.  Superclass * pSuperclass = &testSubclassC;
  115.  SubclassA *pSubclassA = &testSubclassC;
  116.  SubclassB *pSubclassB = &testSubclassC;
  117.  pSubclassA->PolymorphismFun();// call SubclassC::PolymorphismFun()
  118.  //pSubclassB->PolymorphismFun();//this is not work
  119.  //pSuperclass->PolymorphismFun();// this is not work
  120. #endif //INHERIT_SUB
  121.  return true;
  122. }

在VS2005和cygwin上输出都是

点击(此处)折叠或打开

  1. SubclassC::PolymorphismFun
  2. SubclassC::PolymorphismFun
  3. SubclassC::PolymorphismFun
其中, 1,2是由testSubclassC.SubclassB::test()调用输出的; 3是由pSubclassA->PolymorphismFun()调用输出.证明外部调用了子类的private方法. 需要注意的是SubclassA的PolymorphismFun函数是public的,类继承使用public方式,使用其他继承方式同样会发生相应的变化, 如

点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. #define INHERIT_SUB

  4. #ifdef INHERIT_SUB
  5. class Superclass
  6. {
  7. public:
  8.     void test()
  9.     {
  10.         int delay = 0;
  11.         delay = delay;
  12.         PolymorphismFun();
  13.         this->PolymorphismFun();
  14.     }

  15. protected:
  16.     virtual void PolymorphismFun()
  17.     {
  18.         int delay = 0;
  19.         delay = delay;
  20. #pragma message("Superclass::PolymorphismFun")
  21.         cout<<"Superclass::PolymorphismFun"<<endl;
  22.     }

  23.     virtual ~Superclass()
  24.     {
  25.         int delay = 0;
  26.         delay = delay;
  27.     }

  28. };

  29. class SubclassA : private Superclass// change from pulic to private
  30. {
  31. public:
  32.     void test()
  33.     {
  34.         int delay = 0;
  35.         delay = delay;
  36.         PolymorphismFun();
  37.         this->PolymorphismFun();
  38.     }

  39.     void PolymorphismFun()
  40.     {
  41.         int delay = 0;
  42.         delay = delay;
  43. #pragma message("SubclassA::PolymorphismFun")
  44.         cout<<"SubclassA::PolymorphismFun"<<endl;
  45.     }

  46.     virtual void operator()(){
  47.         int delay = 0;
  48.         delay = delay;
  49.     };

  50.     virtual ~SubclassA()
  51.     {
  52.         int delay = 0;
  53.         delay = delay;
  54.     }
  55. };

  56. class SubclassB : public SubclassA
  57. {
  58. public:
  59.     void test()
  60.     {
  61.         int delay = 0;
  62.         delay = delay;
  63.         PolymorphismFun();
  64.         this->PolymorphismFun();
  65.     }

  66. private:
  67.     virtual void PolymorphismFun()=0;

  68. public:
  69.     virtual void operator()()
  70.     {
  71.         int delay = 0;
  72.         delay = delay;
  73.     };

  74.     virtual ~SubclassB()
  75.     {
  76.         int delay = 0;
  77.         delay = delay;
  78.     }
  79. };


  80. class SubclassC : public SubclassB
  81. {
  82. public:
  83.     void test()
  84.     {
  85.         int delay = 0;
  86.         delay = delay;
  87.         PolymorphismFun();
  88.         this->PolymorphismFun();
  89.     }

  90. private:
  91.     void PolymorphismFun()
  92.     {
  93.         int delay = 0;
  94.         delay = delay;
  95. #pragma message("SubclassC::PolymorphismFun")
  96.         cout<<"SubclassC::PolymorphismFun"<<endl;
  97.     }

  98. public:
  99.     virtual void operator()()
  100.     {
  101.         int delay = 0;
  102.         delay = delay;
  103.     };

  104.     virtual ~SubclassC()
  105.     {
  106.         int delay = 0;
  107.         delay = delay;
  108.     }
  109. };
  110. #endif//INHERIT_SUB


  111. int main(int argc, char *argv[])
  112. {
  113. #pragma message("main starting")

  114. #ifdef INHERIT_SUB
  115.     SubclassC testSubclassC;
  116.     testSubclassC.SubclassB::test();
  117.     int nSize = sizeof(testSubclassC);
  118. //    Superclass * pSuperclass = &testSubclassC;// this is not work
  119.     SubclassA *pSubclassA = &testSubclassC;
  120.     SubclassB *pSubclassB = &testSubclassC;
  121.     pSubclassA->PolymorphismFun();
  122.     //pSubclassB->PolymorphismFun();// this is not work
  123.     //pSuperclass->PolymorphismFun();// this is not work
  124. #endif //INHERIT_SUB

  125.     return true;
  126. }
将输出同样的结果.

点击(此处)折叠或打开

  1. SubclassC::PolymorphismFun
  2. SubclassC::PolymorphismFun
  3. SubclassC::PolymorphismFun
这种继承调用体系可以让我们实现当存在用户定义行为时, 让用户继承并实现SubclassC类, 否则就调用默认SubClassA类, SubClassB作为接口,提供给用户继承并要求用户实现相应的接口(不管是private、public、protected都可以被调用). 这种方式可以减少if-else的判断,是模式NULL Object的可选实现方法.
阅读(2689) | 评论(0) | 转发(0) |
0

上一篇:c/c++优秀书目

下一篇:C++ new/delete (一)

给主人留下些什么吧!~~