Didn"t forge 2009-05-13 20:47 采纳率: 25%
浏览 298
已采纳

私有、公有和受保护继承之间的区别

What is the difference between public, private, and protected inheritance in C++? All of the questions I've found on SO deal with specific cases.

转载于:https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance

  • 写回答

15条回答 默认 最新

  • 叼花硬汉 2009-05-13 20:49
    关注

    To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:".

    There are three accessors that I'm aware of: public, protected and private.

    Let:

    class Base {
        public:
            int publicMember;
        protected:
            int protectedMember;
        private:
            int privateMember;
    };
    
    • Everything that is aware of Base is also aware that Base contains publicMember.
    • Only the children (and their children) are aware that Base contains protectedMember.
    • No one but Base is aware of privateMember.

    By "is aware of", I mean "acknowledge the existence of, and thus be able to access".

    next:

    The same happens with public, private and protected inheritance. Let's consider a class Base and a class Child that inherits from Base.

    • If the inheritance is public, everything that is aware of Base and Child is also aware that Child inherits from Base.
    • If the inheritance is protected, only Child, and its children, are aware that they inherit from Base.
    • If the inheritance is private, no one other than Child is aware of the inheritance.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?