C++中public,protected,private派生類繼承問題和訪問權限問題


C++中public,protected,private派生類繼承問題和訪問權限問題

當一個子類從父類繼承時,父類的所有成員成為子類的成員,此時對父類成員的訪問狀態由繼承時使用的繼承限定符決定。

 

1.如果子類從父類繼承時使用的繼承限定符是public,那么
(1)父類的public成員成為子類的public成員,允許類以外的代碼訪問這些成員;
(2)父類的private成員仍舊是父類的private成員,子類成員不可以訪問這些成員;
(3)父類的protected成員成為子類的protected成員,只允許子類成員訪問;

 

2.如果子類從父類繼承時使用的繼承限定符是protected,那么

(1)父類的public成員成為子類的protected成員,只允許子類成員訪問;
(2)父類的private成員仍舊是父類的private成員,子類成員不可以訪問這些成員;

(3)父類的public成員成為子類的protected成員,只允許子類成員訪問

 

 

3.如果子類從父類繼承時使用的繼承限定符是private,那么

(1)父類的public成員成為子類的private成員,只允許子類成員訪問;
(2)父類的private成員仍舊是父類的private成員,子類成員不可以訪問這些成員;
(3)父類的protected成員成為子類的private成員,只允許子類成員訪問;

 

其實這些都很有的規律的,子類public時表示最大的繼承權限是public,所以子類按照原樣繼承,子類protected繼承時最大繼承權限是protected, 所以基類的public成員降級成為protected了....子類private繼承時所以都成為private了, 不過子類不能訪問基類的private成員..

 

子類默認的是private繼承基類...

 

舉個使用private繼承的例子,Boost::Utility庫的不可以復制的類 noncopyable

 

#include "boost/utility.hpp"

或者是

#include "boost/noncopyable.hpp"

 

[cpp]  view plain  copy
 
 print?
  1. #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED  
  2. #define BOOST_NONCOPYABLE_HPP_INCLUDED  
  3.   
  4. namespace boost {  
  5.   
  6. //  Private copy constructor and copy assignment ensure classes derived from  
  7. //  class noncopyable cannot be copied.  
  8.   
  9. //  Contributed by Dave Abrahams  
  10.   
  11. namespace noncopyable_  // protection from unintended ADL  
  12. {  
  13.   class noncopyable  
  14.   {  
  15.    protected:  
  16.       noncopyable() {}  
  17.       ~noncopyable() {}  
  18.    private:  // emphasize the following members are private  
  19.       noncopyable( const noncopyable& );  
  20.       const noncopyable& operator=( const noncopyable& );  
  21.   };  
  22. }  
  23.   
  24. typedef noncopyable_::noncopyable noncopyable;  
  25.   
  26. // namespace boost  
  27.   
  28. #endif  // BOOST_NONCOPYABLE_HPP_INCLUDED  



 

類 boost::noncopyable 被規定為作為私有基類來使用,它可以有效地關閉復制構造和賦值操作:

 

 

[cpp]  view plain  copy
 
 print?
  1. #include "boost/utility.hpp"  
  2.   
  3. class please_dont_make_copies : boost::noncopyable {  
  4. public:  
  5.   void do_stuff() {  
  6.     std::cout << "Dear client, you just cannot copy me!";  
  7.  }  
  8. };  

 

 

這樣就禁止了復制和賦值....

 

 

=========================================================================

c++ big three

 

 

三法則英語rule of threethe Law of The Big ThreeThe Big Three三法則三大定律)在 C++ 程序設計里,它是一個以設計的基本原則而制定的定律,三法則的要求在於,假如類型有明顯地定義下列其中一個成員函數,那么程序員必須連其他二個成員函數也一同編寫至類型內,亦即下列三個成員函數缺一不可。 [1]:

上述三個函數是特別的成員函數,假如程序員沒有自行定義或是編寫聲明它們,那么編譯器會自動地創建它們,並且會編譯至應用程序內。相反地,假如程序員有定義上述三者其中一個函數,那么由編譯器自動產生出來的上述三個函數是不會搭配到這個類型內。三法則(Rule of three)這個專有名詞是由 Marshall Cline 於 1991 年創立的[2]

 

class_a.h文件

 

[cpp]  view plain  copy
 
 print?
  1. #ifndef _CLASS_A_H_  
  2. #define _CLASS_A_H_  
  3.    
  4. #ifndef _MSC_VER  
  5. #undef NULL  
  6. #define NULL 0  
  7. #endif  
  8. #include <iostream>  
  9. #include <cstdlib>  
  10. #define BUFFER_SIZE 7  
  11.    
  12. using namespace std;  
  13.    
  14. class ClassA  
  15. {  
  16. public:  
  17.     // 三種建構子  
  18.     ClassA()  
  19.     {  
  20.         cout<<"ClassA():"<<endl;  
  21.         this->setAlloc(BUFFER_SIZE);  
  22.         this->setData();  
  23.     }  
  24.     ClassA(const int n)  
  25.     {  
  26.         cout<<"ClassA(const int n):"<<endl;  
  27.         this->setAlloc(n);  
  28.         this->setData();  
  29.     }  
  30.     // 複製建構子  
  31.     ClassA(const ClassA& clone)  
  32.     {  
  33.         cout<<"ClassA(const ClassA& clone):"<<endl;  
  34.         this->setAlloc(clone.m_N);  
  35.         this->setData(clone.m_pn);  
  36.     }  
  37.     // 複製指定運算子成員函式  
  38.     ClassA& operator=(const ClassA& clone)  
  39.     {  
  40.         cout<<"ClassA& operator=(const ClassA& clone)"<<endl;  
  41.         // 保護:禁止自己設值給自己  
  42.         if ( this != &clone )  
  43.         {  
  44.             this->setData(clone.m_pn);  
  45.         }  
  46.         return *this;  
  47.     }  
  48.     // 解構子  
  49.     ~ClassA()  
  50.     {  
  51.         cout<<"~Destructor!!!"<<endl;  
  52.         // 釋放記憶體  
  53.         delete [] this->m_pn;  
  54.     }  
  55.     // 配置  
  56.     void setAlloc(const int n)  
  57.     {  
  58.         this->m_N = n;  
  59.         // 配置一塊記憶體給指標  
  60.         this->m_pn = new int[this->m_N];  
  61.     }  
  62.     // 填入一堆的整數值  
  63.     void setData(int* pn = NULL)  
  64.     {  
  65.         for ( int i = 0; i < this->m_N; i ++)  
  66.         {  
  67.             // 給初始值  
  68.             if ( pn == NULL )  
  69.             {  
  70.                 this->m_pn[i] = (2 * i + 1);  
  71.             }  
  72.             // 複製指標儲存的整數值  
  73.             else  
  74.             {  
  75.                 this->m_pn[i] = pn[i];  
  76.             }  
  77.         }  
  78.     }  
  79.     // 列印顯示  
  80.     void print(void)  
  81.     {  
  82.         for ( int i = 0; i < this->m_N; i ++)  
  83.         {  
  84.             cout<<" "<<this->m_pn[i];  
  85.         }  
  86.         cout<<endl;  
  87.     }  
  88. private:  
  89.     // 指標  
  90.     int* m_pn;  
  91.     // 元素個數  
  92.     int m_N;  
  93. };  
  94.    
  95. #endif  



 

主函數

 

[cpp]  view plain  copy
 
 print?
  1. // Headers and Macros  
  2. #ifndef _MSC_VER  
  3. #undef NULL  
  4. #define NULL 0  
  5. #endif  
  6. #include <iostream>  
  7. #include <cstdlib>  
  8. #include "class_a.h"  
  9. using namespace std;  
  10. //  
  11. //Main Function  
  12. #ifndef _MSC_VER  
  13. int  
  14. #else  
  15. void  
  16. #endif  
  17. main(int argc, char** argv)  
  18. {  
  19.     // 區塊  
  20.     {  
  21.         // 建立第一個物件  
  22.         ClassA A(BUFFER_SIZE);  
  23.         cout<<" A =>";  
  24.         A.print();  
  25.         {  
  26.             // 開始執行 ClassA(const ClassA& clone)  
  27.             ClassA B = A;  
  28.             cout<<" B =>";  
  29.             B.print();  
  30.         }  
  31.         {  
  32.             ClassA C;  
  33.             // 開始執行 ClassA& operator=(const ClassA& clone)  
  34.             C = A;  
  35.             cout<<" C =>";  
  36.             C.print();  
  37.         }  
  38.     }  
  39.     system("PAUSE");  
  40.     return  
  41. #ifndef _MSC_VER  
  42.         EXIT_SUCCESS  
  43. #endif  
  44.         ;  
  45. }  



 

http://zh.wikipedia.org/wiki/%E4%B8%89%E6%B3%95%E5%89%87_(C%2B%2B%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM