C++ 中的友元范围
如果我有三个类,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我是在等待回复的时候发现这篇文章的。 它很好地回答了我的问题:
C++ 中的友元范围
I just found this article while waiting for replies. It answers my question pretty well:
Friend scope in C++
因为在 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.
这一切都在这里总结得很好:
-->
来自
http://yosefk.com/c++fqa/friend.html# fqa-14.4
This is all well summed-up here:
-->
from
http://yosefk.com/c++fqa/friend.html#fqa-14.4
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.
C++ 中的友谊不是传递性的:
而且,友谊不是对称的。
您必须明确声明 A 是 C 的友元,才能从 A 内部访问 C 的私有内容。如果向类添加 setter 和 getter 会暴露不应该公开的信息,则您应该考虑朋友(如果您不能)发现你的设计有缺陷(使用朋友是有效的。这并不是糟糕设计的标志)。 如果您可以添加 setter 和 getter 而不会破坏界面,那么您应该避免与其他类成为朋友。 请注意,嵌套类始终是嵌套类的友元。 因此,嵌套类可以看到嵌套类的私有属性。
Friendship in C++ is not transitive:
Also, friendship is not symmetric.
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.