【问题标题】:Are derived classes considered friends?派生类是否被视为朋友?
【发布时间】:2011-05-04 06:53:02
【问题描述】:

如果我创建基类 A 并且 A 是类 B 的朋友,那么从 A 派生的类是否可以根据自己的喜好访问 B,否则允许什么?

谢谢

【问题讨论】:

    标签: c++ inheritance friend


    【解决方案1】:
    struct A{};
    
    struct Ader : A{};
    
    struct B{
       friend struct A;
    };
    

    没有。 C++ 中不继承友谊。它也不是传递的。

    Ader 不能以好友身份访问 B,除非 B 明确给予友谊,因为它的基础 AB 的好友。

    【讨论】:

      【解决方案2】:

      不,这是不允许的。检查here

      【讨论】:

        猜你喜欢
        • 2012-12-04
        • 1970-01-01
        • 2018-01-05
        • 2020-08-10
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 2014-06-18
        相关资源
        最近更新 更多