c++面试在线测试中的操作员过载问题
我最近看到一个面试在线测试问题,我需要帮助。这不是我正在面试的工作的问题,我只是对答案感到好奇。提前致谢。
#include <iostream>
class Foo
{
//...
};
void staff(Foo& f)
{
//..
std::out << f << endl; // output Foo object f
}
问题是:
需要定义哪些运算符才能正确显示对象?
的成员函数std::ostream&运算符 <<(std::ostream&)
作为类Foo
std::streambuf&运算符 << (std::ostream&, Foo const&)
作为独立的重载运算符。<代码>无效<<运算符 <<(std::ostream& , Foo const&) 作为独立的重载
std::ostream&运算符 << (std::ostream&, Foo const&)
作为独立的、重载的void std::operator<<(std::ostream&)
作为成员函数类Foo
I recently saw an interview online test question that I need help with. This is not a question for a job I'm interviewing for, I was just curious about the answer. Thanks in advance.
#include <iostream>
class Foo
{
//...
};
void staff(Foo& f)
{
//..
std::out << f << endl; // output Foo object f
}
The question was:
What operators need to be defined in order to show object properly?
std::ostream& operator <<(std::ostream&)
as a member function of classFoo
std::streambuf& operator << (std::ostream&, Foo const&)
as a stand alone, overloaded operator.void << operator<<(std::ostream& , Foo const&)
as a stand alone, overloadedstd::ostream& operator << (std::ostream&, Foo const&)
as a stand alone, overloadedvoid std::operator<<(std::ostream&)
as a memeber function of classFoo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用上述签名定义
operator<<
。You should define
operator<<
with the above signature.