派生类是否被视为朋友? [英] Are derived classes considered friends?

查看:104
本文介绍了派生类是否被视为朋友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建基类A而A是B类的朋友,那么一个类可以从A访问B派生出来,或者它允许什么?



谢谢

解决方案

  struct A {}; 

struct Ader:A {};

struct B {
friend struct A;
};

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



Ader 无法访问 B 作为朋友,除非B明确给予友谊,只因为它的基础 A B 的朋友。 / p>

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?

Thanks

解决方案

struct A{};

struct Ader : A{};

struct B{
   friend struct A;
};

No. Friendship is not inherited in 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.

这篇关于派生类是否被视为朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆