简体   繁体   English

派生类是否被视为朋友?

[英]Are derived classes considered friends?

If I create base class A and A is a friend of class B, can a class derived from A access B to its liking, or else what is it allowed? 如果我创建基类A而A是B类的朋友,那么一个类可以从A访问B派生出来,或者它允许什么?

Thanks 谢谢

struct A{};

struct Ader : A{};

struct B{
   friend struct A;
};

No. Friendship is not inherited in C++. 不。友谊不是在C ++中继承的。 It is also not transitive. 它也不是传递性的。

Ader can not access B as a friend unless explicitly given friendship by B, just because it's base A is a friend of B . 除非B明确给予友谊,否则Ader不能作为朋友访问B ,因为它的基础AB的朋友。

No, that's not allowed. 不,那是不允许的。 Check here . 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM