超载<< c++ 中的运算符
嘿,我得到了一些我无法理解的东西,有两种类型的重载此运算符的解决方案:1 是在方法的开头包含朋友,另一种不包含朋友。 我非常希望有人能解释一下它们之间的区别,优点/缺点。 例如重载运算符 <<在理性课堂上:
class Rational:
{
private: int m_t,m_b;
...
friend ostream& operator<<(ostream& out,const Rational& r) // option 1
{ return out << r.m_t << "/" <<r.m_b;} // continue of option 1
ostream& operator<<(ostream& out,const Rational& r){return r.print();} // option 2
virtual ostream& print(ostream& out) const // continue of option 2
{ //
return out<<m_t << "/" << m_b;
} //
};
我被告知第二个选项不正确,如果有人可以纠正我,我将非常感激。 提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答:选项 #2 实际上不是一个选项,而是一个语法错误,因为它试图将二元运算符定义为传递两个操作数的成员。
稍微长一点的答案:如果您将第二个操作数设置为自由函数(不是类的成员),那么这将起作用。哪一种更可取取决于具体情况和您的喜好。对于初学者:第一个的缺点是它允许
operator<<
访问Rational
中的所有内容(包括私有辅助函数),而第二个的缺点是您向类的公共 API 引入了一个没有人需要的函数。The short answer: Option #2 actually isn't an option, but a syntax error, because it tries to define a binary operator as a member passing two operands.
The somewhat longer answer: If you make the second operand a free function (not a member of the class), this will work. Which one is preferable depends on the circumstances and your preferences. For starters: The disadvantage of the first is that it allows
operator<<
to access everything inRational
(including private helper functions), while the disadvantage of the second is that you introduce a function to the class' public API that nobody needs.operator<<
(对于ostream
)需要是一个自由函数(因为左侧参数是一个流,而不是您的类)。friend
关键字使其成为一个自由函数(可以访问私有成员的自由函数)。然而,如果这个功能可以通过公共接口来实现,那么最好这样做,并且只使用非友元免费功能。
operator<<
(forostream
) needs to be a free function (since the left-hand argument is a stream, not your class).The
friend
keyword makes it a free function (a free function that has access to the private members).However, if this functionality can be implemented in terms of the public interface, it is better to do so and just use a non-friend free function.
考虑一个应该输出 Rational 的 num 和 den 的函数:
不幸的是,这只是一个全局函数。与任何其他全局函数一样,它无法访问
Rational
的私有成员。要使其与Rational
对象一起工作,您需要使其成为Rational
的friend
:friend ostream& 对象。
表明Rational
类中的operator<<(ostream&out, const Rational& r);ostream& operator<<(ostream&out, const Rational&r)
函数可以直接使用Rational
的私有成员。现在,当您编写时:
将进行以下函数调用:
您可以将
operator<<
编写为Rational
的成员函数吗?这是不可能的,因为上面的转换中cout
必须是第一个参数。如果您将operator<<
作为Rational
的成员:您需要这样调用它:
这很丑陋。
Consider a function that should output the num and den of
Rational
:Unfortunately, this is just a global function. Like any other global function, it cannot access the private members of
Rational
. To make it work withRational
objects, you need to make itfriend
ofRational
:The
friend ostream& operator<<(ostream& out, const Rational& r);
insideRational
class indicates thatostream& operator<<(ostream& out, const Rational& r)
function can directly useRational
's private members.Now when you write:
the following function call is made:
Can you write
operator<<
as a member function ofRational
? That's simply not possible because of the above conversion wherecout
has to be first parameter. If you makeoperator<<
as a member ofRational
:you need to call it this way:
which is ugly.