11.5 - Inheritance and access specifiers

本文详细解析了C++中类继承过程中的访问限定符(public、private、protected),包括它们如何影响成员的可见性和派生类的访问权限。重点介绍了三种继承类型(public、private、protected)及其在不同场景下的应用,以及如何在派生类中正确使用这些限定符。

1. Protected access specifier限定类成员只能被同一个class或derived class的成员函数访问

class Base
{
public:
    int m_nPublic; // can be accessed by anybody
private:
    int m_nPrivate; // can only be accessed by Base member functions (but not derived classes)
protected:
    int m_nProtected; // can be accessed by Base member functions, or derived classes.
};
 
class Derived: public Base
{
public:
    Derived()
    {
        // Derived's access to Base members is not influenced by the type of inheritance used,
        // so the following is always true:
 
        m_nPublic = 1; // allowed: can access public base members from derived class
        m_nPrivate = 2; // not allowed: can not access private base members from derived class
        m_nProtected = 3; // allowed: can access protected base members from derived class
    }
};
 
int main()
{
    Base cBase;
    cBase.m_nPublic = 1; // allowed: can access public members from outside class
    cBase.m_nPrivate = 2; // not allowed: can not access private members from outside class
    cBase.m_nProtected = 3; // not allowed: can not access protected members from outside class
}

2. 类的继承有3种不同的方式: public, private, 和 protected

// Inherit from Base publicly
class Pub: public Base
{
};
 
// Inherit from Base privately
class Pri: private Base
{
};
 
// Inherit from Base protectedly
class Pro: protected Base
{
};
 
class Def: Base // Defaults to private inheritance
{
};

如最后一个所示,如果没有指定继承的类型,默认为private继承。

由以上两点,我们得到9种不同的组合:3 member access specifiers (public, private, and protected), and 3 inheritance types (public, private, and protected).

Public inheritance

这是最常见的一种继承类型,实际上其他几种则非常少见。当你以public的方式继承基时,所有的成员保持原有的访问限制,private成员仍是private,protected成员仍是protected,public成员仍是public。

class Base
{
public:
    int m_nPublic;
private:
    int m_nPrivate;
protected:
    int m_nProtected;
};
 
class Pub: public Base
{
    // Public inheritance means:
    // m_nPublic stays public
    // m_nPrivate stays private
    // m_nProtected stays protected
 
    Pub()
    {
        // The derived class always uses the immediate parent's class access specifications
        // Thus, Pub uses Base's access specifiers
        m_nPublic = 1; // okay: anybody can access public members
        m_nPrivate = 2; // not okay: derived classes can't access private members in the base class!
        m_nProtected = 3; // okay: derived classes can access protected members
    }
};
 
int main()
{
    // Outside access uses the access specifiers of the class being accessed.
    // In this case, the access specifiers of cPub.  Because Pub has inherited publicly from Base,
    // no access specifiers have been changed.
    Pub cPub;
    cPub.m_nPublic = 1; // okay: anybody can access public members
    cPub.m_nPrivate = 2; // not okay: can not access private members from outside class
    cPub.m_nProtected = 3; // not okay: can not access protected members from outside class
}

值得注意的是

1) Derived class不能直接访问基类的private成员

2) Derived class可以直接访问基类的protected成员,前提是不会把这些成员暴露给public

3) Derived class uses access specifiers from the base class

4) Outside uses access specifiers from the derived class


Private inheritance

Private inheritance方式下,基类的所有成员都被归为private。
应该注意的是这种继承方式不影响派生类对所继承成员的访问,只影响试图通过派生类来访问继承成员的代码
class Base
{
public:
    int m_nPublic;
private:
    int m_nPrivate;
protected:
    int m_nProtected;
};
 
class Pri: private Base
{
    // Private inheritance means:
    // m_nPublic becomes private
    // m_nPrivate stays private
    // m_nProtected becomes private
 
    Pri()
    {
        // The derived class always uses the immediate parent's class access specifications
        // Thus, Pub uses Base's access specifiers
        m_nPublic = 1; // okay: anybody can access public members
        m_nPrivate = 2; // not okay: derived classes can't access private members in the base class!
        m_nProtected = 3; // okay: derived classes can access protected members
    }
};
 
int main()
{
    // Outside access uses the access specifiers of the class being accessed.
    // Note that because Pri has inherited privately from Base,
    // all members of Base have become private when access through Pri.
    Pri cPri;
    cPri.m_nPublic = 1; // not okay: m_nPublic is now a private member when accessed through Pri
    cPri.m_nPrivate = 2; // not okay: can not access private members from outside class
    cPri.m_nProtected = 3; // not okay: m_nProtected is now a private member when accessed through Pri
 
    // However, we can still access Base members as normal through Base:
    Base cBase;
    cBase.m_nPublic = 1; // okay, m_nPublic is public
    cBase.m_nPrivate = 2; // not okay, m_nPrivate is private
    cBase.m_nProtected = 3; // not okay, m_nProtected is protected
}

Protected inheritance

这种继承方式非常罕见。在这种继承方式下,public和protected成员变成protected,private成员仍是private

Public inheritance
Base access specifierDerived access specifierDerived class accessPublic access
PublicPublicYesYes
PrivatePrivateNoNo
ProtectedProtectedYesNo


Private inheritance
Base access specifierDerived access specifierDerived class accessPublic access
PublicPrivateYesNo
PrivatePrivateNoNo
ProtectedPrivateYesNo


Protected inheritance
Base access specifierDerived access specifierDerived class accessPublic access
PublicProtectedYesNo
PrivatePrivateNoNo
ProtectedProtectedYesNo

总结

1) 基类总是可以访问自己的成员,access specifiers仅仅影响派生类和其他函数的访问权限
2) 派生类根据immediate parent的access specifier来访问继承的成员,派生类型不影响派生类的访问
3) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值