在 C++ 中,向类添加友元是否会改变其内存布局?
另外,你在班级的哪个位置宣布朋友重要吗? 添加友元类或友元函数有什么关系吗?
Also, does it matter where in the class you declare the friend ?
Does it matter if you add a friend class or a friend function ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有。这纯粹是编译时的事情:类似于访问修饰符本身。
尽管您在类中编写了声明,但您并没有真正将友元添加到类中。您基本上将其他内容声明为该类的友元,并简单地允许它访问该类的私有成员,就像它们是公共成员一样。
No it doesn't. It's a purely compile-time thing: similar to access modifiers themselves.
Despite the fact that you write the declaration inside the class, you don't really add a friend to a class. You'd basically declare something else as a friend of the class and simply allow it to access the class's private members, as if they were public.
正如已经提到的,它纯粹是一种编译时机制。
As mentioned already, it is purely a compile-time mechanism.