1
基類訪問規範與成員訪問規範有什麼不同?基類訪問規範與類成員訪問規範
基類訪問規範與成員訪問規範有什麼不同?基類訪問規範與類成員訪問規範
基類訪問規範決定有關基類的成員您類訪問規範。他們提供了一種方法來隱藏基類的成員,如果你不希望他們公開在你的班級。儘管如此,它們不會影響班級成員的可見度。
C++ FAQ解釋這個問題相當不錯。
class Base
{
protected:
int A;
public:
int B;
};
class Derived1 : public Base
{
// Derived1::A outside class is seen as protected
// Derived1::B outside class is seen as public
};
class Derived2 : protected Base
{
// Derived1::A outside class is seen as protected
// Derived1::B outside class is seen as protected
};
class Derived3 : private Base
{
// Derived1::A outside class is seen as private
// Derived1::B outside class is seen as private
};