【发布时间】:2010-11-11 12:43:33
【问题描述】:
我在 joelonsoftware.com http://discuss.joelonsoftware.com/default.asp?joel.3.594503.11 的存档文件中发现了这个问题
“嗨,
我今天过得特别慢,而且 无法理解接线员 超载问题。我想要一个 类能够通过 插入运算符,即:
myClassInstance
在内部,我希望一切都结束 在一个字符串流中,这样我就可以了 将其种植到其他溪流(例如 std::cout 和一个 ofstream)。我有 非常困惑我怎么能做到这一点 无需编写运算符
感谢您的帮助!”
这正是我想要做的。通过定义模板和另一个重载方法来处理像在 ostream 类中定义的 endl 这样的操纵器,我找到了处理所有类型的方法。
UIStream& UIStream ::operator << (const T str)
{
CString cstr(stringify(str).c_str());
theFrame->m_pOutputView->WriteMessage(cstr);
return *this;
}
//for manipulators like std::endl
UIStream& UIStream ::operator <<(ostream& (*m)(ostream&))
{
//stream<<*m;
//CString cstr((*m)(new ostream).c_str());
if(*m==&std::endl);
theFrame->m_pOutputView->WriteMessage("\n");
return (*this);
}
我仍在与接受 ios_base 中定义的 hex dec 或 oct 等参数的操纵器作斗争。
【问题讨论】:
-
我真的很喜欢你的问题的答案,即std::endl is of unknown type when overloading operator<<
-
同一用户已经在两个帖子中处理了这个问题。 Kazoom,请阅读答案!
-
你确定吗?我认为这个问题与处理带有参数的操纵器有关,据我所知,其他问题并未涵盖这一点。虽然说 hex、dec 和 oct 不是带参数的操纵器...
-
这个问题之前已经回答过(在我的回答中还有其他人:stackoverflow.com/questions/1133739/…)