C++ 中的友元范围

发布于 2024-07-12 05:06:40 字数 145 浏览 9 评论 0原文

如果我有三个类,A,B,C。A和B是朋友(双向)。 另外,B 和 C 是朋友(双向)。 A有一个指向B的指针,B有一个指向C的指针,为什么A不能通过指针访问C的私有数据?

只是澄清一下:这是一个纯粹的理论 C++ 语言问题,而不是一个设计建议问题。

If I have three classes, A, B, C. A and B are friends (bidirectionally). Also, B and C are friends (bidirectionally). A has a pointer to B and B has a pointer to C. Why can't A access C's private data through the pointer?

Just to clarify: This is a pure theoretical C++ language question, not a design advice question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻之岛 2024-07-19 05:06:41

我是在等待回复的时候发现这篇文章的。 它很好地回答了我的问题:
C++ 中的友元范围

I just found this article while waiting for replies. It answers my question pretty well:
Friend scope in C++

清君侧 2024-07-19 05:06:41

因为在 C++ 中,友谊不是传递属性。 实际上,应该尽可能避免它,因为它会引入系统的复杂性。

想象一下,B 是一个中介类,A 和 C 是需要管理的组件,您真的认为按钮应该需要访问复选框的实现吗?

顺便说一句,在您询问的情况下,我不知道您的头衔的“层次结构”在哪里。

Because in C++ friendship is not a transitive property. Actually it should be avoided whenever possible because it introduces complexity in a system.

Imagine that B is a mediator class and A and C are components that need to be managed, do you really think it makes sense that a button should need access to the implementation of a checkbox?

By the way, I don't see where the 'hierarchy' of your title is in the case you ask.

朕就是辣么酷 2024-07-19 05:06:41

这一切都在这里总结得很好:

What does it mean that "friendship isn't inherited, transitive, or reciprocal"?

-->

这意味着从友元类派生的类不会
自动成为朋友(你信任你朋友的孩子吗?),
朋友的朋友不会自动成为朋友(你是吗?
信任你朋友的朋友?),并且一个类声明
另一个类作为“朋友”不会自动成为
那个班级(你相信那些称你为朋友的人吗?)。

来自

http://yosefk.com/c++fqa/friend.html# fqa-14.4

This is all well summed-up here:

What does it mean that "friendship isn't inherited, transitive, or reciprocal"?

-->

It means that classes derived from a friend class don't
automatically become friends (do you trust the kids of your friends?),
a friend of a friend doesn't automatically become a friend (do you
trust the friends of your friends?), and that a class declaring
another class as "friend" doesn't automatically become a friend of
that class (do you trust anyone who calls you a friend?).

from

http://yosefk.com/c++fqa/friend.html#fqa-14.4

沧桑㈠ 2024-07-19 05:06:40

C++ 中的友谊不是传递性的:

约翰是我的朋友,他可以随时使用我的无线连接(我信任他)。
约翰的朋友蒂姆虽然是个浪费者,虽然约翰是我的朋友,但我不将蒂姆列为朋友,因此我不让他使用我的无线连接。

友谊不是遗传的

,约翰的孩子也是一群流氓,所以我不相信他们,要么他们绝对不是我的朋友,也不是我自己的孩子,我尽我所能地信任他们。

尽管我们的孩子无法直接访问无线网络,但如果他们通过我们就可以访问无线网络。 因此,如果约翰的孩子通过约翰访问我的无线网络,他们就可以访问我的无线网络(即他们受到约翰的监督和保护)。

而且,友谊不是对称的。

约翰有一份政府工作,因此不幸的是他不能信任任何人,尤其是在无线方面。

你永远是你自己最好的朋友。

这允许像复制构造函数这样的东西,即使没有真正的访问,您也可以访问另一个对象的私有成员。

所以我也自动成为我所有克隆人的朋友:-),因为它们只是我自己的其他实例。

Friendship in C++ is not transitive:

John is a friend of mine and he can use my wireless connection any time (I trust him).
John's friend Tim though is a waster and though John is my friend I do not include Tim as a friend, and thus I don't let him use my wireless connection.

Friendship is NOT inherited

Also John's children are a bunch of hooligans so I don't trust them either they are definitely not my friends nor are my own children who I trust as far as I could throw them.

Though our children can not directly accesses the wireless they can get access to it if they go through us. So John's children can access my wireless if they access it via John (ie they are supervised and protected by John).

Also, friendship is not symmetric.

John has a goverment job so he unfortunately is not allowed to trust anyone, especially when it comes to wireless.

You are always your own best friend.

This allows things like copy constructors where you can access the private member of another object even though there is no real accesses.

So I am also automatically friends with all my clones :-) as they are just other instances of myself.

桃扇骨 2024-07-19 05:06:40

C++ 中的友谊不是传递性的:

(A is friend of B) and (B is friend of C) does not mean (A is friend of C)

而且,友谊不是对称的。

(A is friend of B) does not mean (B is friend of A) 

您必须明确声明 A 是 C 的友元,才能从 A 内部访问 C 的私有内容。如果向类添加 setter 和 getter 会暴露不应该公开的信息,则您应该考虑朋友(如果您不能)发现你的设计有缺陷(使用朋友是有效的。这并不是糟糕设计的标志)。 如果您可以添加 setter 和 getter 而不会破坏界面,那么您应该避免与其他类成为朋友。 请注意,嵌套类始终是嵌套类的友元。 因此,嵌套类可以看到嵌套类的私有属性。

Friendship in C++ is not transitive:

(A is friend of B) and (B is friend of C) does not mean (A is friend of C)

Also, friendship is not symmetric.

(A is friend of B) does not mean (B is friend of A) 

You have to explicitly state that A is a friend of C to be able to access C's private stuff from within A. If adding a setter and getter to a class exposes information not meant to be exposed, you should consider friends if you can't find your design being faulty (using friend is valid. It's not a sign for bad design). If you can add a setter and getter without that being destructive to the interface, then you should avoid making other classes friends. Note that a nested class is always a friend of the nesting class. So a nested class can see the privates of the nesting class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文