我在 joelonsoftware.com 的存档文件中发现了这个问题 http://discuss.joelonsoftware .com/default.asp?joel.3.594503.11
“嗨,
我今天过得很慢,而且
我无法理解操作员
超载问题。 我想要一个
类能够通过一个接受数据
插入运算符,即:
myClassInstance << std::字符串(“一个
字符串") << 4 << 3.4 << std::endl;
内心深处,我希望一切都能结束
在字符串流中,这样我就可以
将其转移到其他溪流(例如
std::cout 和一个 ofstream)。 我有
我非常困惑我怎么能做到这一点
无需编写运算符<<
每种数据类型的重载,以及如何重载
将创建一个输入流
第一次调用 (myClassInstance <<
...)。
非常感谢您的帮助!”
这正是我想要做的。我已经找到了通过定义模板和另一个重载方法来处理所有类型的方法来处理像 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 中定义的。
I found this question on archived file at joelonsoftware.com http://discuss.joelonsoftware.com/default.asp?joel.3.594503.11
"Hi,
I'm having a particularly slow day and
can't get my head round an operator
overloading problem. I would like a
class to be able to accept data via an
insertation operator, i.e:
myClassInstance << std::string("a
string") << 4 << 3.4 << std::endl;
Internally, I'd like everything to end
up in a stringstream so I can then
farm it off to other streams (say
std::cout and an ofstream). I have got
horribly confused how I can do this
without having to write an operator<<
overload for every data type, and how
an input stream would be created on
the first call (myClassInstance <<
...).
Any help gratefully received!"
This is exactly what I am trying to do. I have found my way to deal with all types by defining templates and another overloaded method to deal with manipulators like endl defined in ostream class.
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);
}
I am still struggling with manipulators that take arguments like hex dec or oct this are defined in ios_base.
发布评论
评论(4)
可能是阅读一本有关该主题的好书的想法。 我推荐 Langer 和 Kreft 的 Standard C++ IOStreams and Locales。
Probably an idea to read a good book on the topic. I recommend Standard C++ IOStreams and Locales by Langer and Kreft.
要使您的流与带参数的操纵器一起工作并不像没有参数时那么简单。 问题是操纵器的形式
如下:
ImpDefClass
,顾名思义,是一个实现定义的类。 例如,在我的系统上,set precision
被声明为其中
_Set precision
只是我的实现定义自身的一个struct
。因此,问题是您不能仅仅编写一个新的流运算符,
因为
XXX
是实现定义的类。 除了定义自己的操纵器来执行相同的任务或将代码硬连接到特定的实现之外,我不知道如何解决这个问题。To make your stream work with manipulators that take arguments is not quite as straightforward as when they do not have arguments. The problem is that the maniuplator will be of the form
where
ImpDefClass
is, as its name suggests, an implementation defined class. For example, on my system,setprecision
is declared aswhere
_Setprecision
is just astruct
that my implementation defines itself.The problem therefore is that you can't just write a new stream operator like
because
XXX
is the implementation defined class. I'm not sure how to get around that other than define your own manipulators to perform the same tasks or hardwire your code to specific implementations.我的方法就是在 MyClass 中包含一个 std::stringstream ,并重载 << 将 MyClass 作为 lhs,将任何内容作为 RHS。
编辑:我想这不完全是你正在寻找的。 对于更简单的用途仍然有用。
The way I would go is just to include a std::stringstream in MyClass, and overload << to take a MyClass as lhs and anything as RHS.
edit: I guess it's not exactly what you are looking for. Still could be useful for simpler uses.
你问的问题越多,我就越相信解决你的问题的正确方法是编写一个新的streambuf。 网上有相关文档 - 在这里解释它对我来说有点太长 - 并且有一个名为 iostreams 的 boost 库可以帮助你。
The more questions you ask, the more I'm convinced that the correct approach to your problem is writing a new streambuf. There is documentation for that on the net -- explaining it here would be a little too long for me -- and there is a library of boost called iostreams which can help you.