友元函数,cpp

发布于 2024-12-06 03:02:12 字数 233 浏览 0 评论 0原文

我们在学校有一项作业,实现一个重载所有算术运算符的 Matrix 类。我所做的是将 += 定义为成员函数,然后将 + 定义为使用 += 函数的非成员函数(两者都在同一个文件中,但 + 在类之外)。 学校工作人员做了类似的事情,只是他们将“+”声明为友元函数(并且还使用了 += 的实现)。

由于这两种实现都工作得很好,我试图理解友元函数给了我什么非成员函数没有的东西? 我什么时候应该更喜欢其中一个?

谢谢! 约塔姆

We had an assignment in school implementing a Matrix class that overloads all arithmetic operators. What I did was to (for example) define += as a member function, and then define + as a non-member function that uses += function (both in the same file, but + outside the class).
The school staff did something similar, only they've declared '+' as a friend function (and also used the implementation of +=).

Since both implementation work perfectly, I am trying to understand what does a friend function gives me that a non member function does not?
When should I prefer each over the other?

Thanks!
Yotam

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

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

发布评论

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

评论(3

呆° 2024-12-13 03:02:12

最好不要声明函数友元,如果它们可以根据类的公共接口实现(例如根据成员operator+=实现operator+)。

有时会以某种方式使用运算符人们倾向于认为,当实现为自由函数时,它们需要自动声明为友元,例如,您可能会听说 operator<< 不能实现为成员函数(因为左侧操作数是一个)。 ostream),因此需要实际上,如果它需要访问私有/受保护的成员和成员函数,那么它只需要成为一个朋友

(我怀疑这可能是因为运算符重载,因为它们的特殊性。调用语法,感觉不像普通函数,并且似乎与其需要在类定义中表达的操作数有某种神奇的联系。)

It is preferable not to declare functions friends, if they can be implemented in terms of the class's public interface (such as operator+ in terms of member operator+=.

Somehow with operators sometimes people tend to assume that when implemented as free functions they need to be automatically declared friends. E.g you might hear that operator<< cannot be implemented as a member function (because the left-hand operand is an ostream), hence it needs to be a free friend function. In reality it only needs to be a friend if it needs access to private/protected members and member functions.

(I suspect that might be because overloaded operators, due to their special call syntax, don't feel like normal functions and seem to have some kind of magical bond with its operands that needs to be expressed in the class definition.)

陌伤浅笑 2024-12-13 03:02:12

朋友版本可以访问您班级的成员。一个普通的非会员则不会。这很有用。

The friend version has access to the members of your class. A plain non-member does not. This can be useful.

白昼 2024-12-13 03:02:12

通过阅读友元函数的定义,您将得到问题的答案。

友元函数在面向对象编程中使用,允许从外部访问类中的私有或受保护的数据。通常,不是类成员的函数无法访问此类信息;外部类也不能。有时,这种访问对程序员来说是有利的。在这种情况下,可以使用friend关键字将函数或外部类声明为该类的友元。然后,函数或外部类将可以访问类内的所有信息(公共、私有或受保护)。

By reading the definition of a Friend Function you will get the answer to your question.

friend function is used in object-oriented programming to allow access to private or protected data in a class from the outside. Normally, a function that is not a member of a class cannot access such information; neither can an external class. Occasionally, such access will be advantageous for the programmer. Under these circumstances, the function or external class can be declared as a friend of the class using the friend keyword. The function or external class will then have access to all information – public, private, or protected – within the class.

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