ostringstream和write() [英] ostringstream and write()

查看:58
本文介绍了ostringstream和write()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ostringstream,缓冲区会增加,以便在需要时至少在调用<<时需要附加数据
。我在

的印象下写了()也做了但是我遇到的问题让我感到怀疑。我可以依赖这样的行为吗?


ostringstream os;


os.write(这是一个测试。 ;,strlen(这是一个测试。);

cout<< os.str()<< endl;


如果是这样,我的问题显然是由别的东西造成的......

解决方案

< blockquote>
ro**********@gmail.com 写道:

使用ostringstream缓冲区增加,以便在需要时至少在调用<<时附加数据。我的印象是写()也有,但我遇到了让我怀疑的问题。我可以依赖这样的行为吗?

ostringstream os;

os.write(这是一个测试。,strlen(" This is is测试。));
cout<< os.str()<< endl;

如果是这样,我的问题显然是由别的东西造成的......




我意识到问题所在。我使用的是iostream作为多态的
实体而且它不是。上面的代码看起来更像是这个

逻辑上无效的代码:


ostringstream os;

ostream * ptr =& os ;


ptr->写(这是一个测试。,strlen(这是一个测试。));

这当然不起作用,因为模板没有虚拟

函数和流是模板。我又傻了。


ro **********@gmail.com 写道:

ro ********** @ gmail.com 写道:

使用ostringstream缓冲区增加以允许数据为
至少在你打电话给<<时需要时追加。我的印象是写()也有,但我遇到了让我怀疑的问题。我可以依赖这样的行为吗?

ostringstream os;

os.write(这是一个测试。,strlen(" This is is测试。));
cout<< os.str()<<如果是这样,我的问题显然是由别的东西造成的......
我意识到问题是什么。我使用iostream作为多态的实体而且它不是。




你是如何得到这个想法的? iostream是多态的。

上面的代码看起来更像是逻辑上无效的代码:

ostringstream os;
ostream * ptr =& os;

ptr->写(这是一个测试。,strlen(这是一个测试。);

这当然不起作用,因为模板没有虚拟的功能和流是模板。




再次,这是一个错误的假设。是什么让你认为一个类

模板不能有虚函数?




Rolf Magnus写道:

ro ********** @ gmail.com 写道:

ro ******** **@gmail.com 写道:

使用ostringstream缓冲区增加,以便在需要时至少在调用<<时附加数据。我的印象是写()也有,但我遇到了让我怀疑的问题。我可以依赖这样的行为吗?

ostringstream os;

os.write(这是一个测试。,strlen(" This is is测试。));
cout<< os.str()<<如果是这样,我的问题显然是由别的东西造成的......
我意识到问题是什么。我使用iostream作为多态的实体而且它不是。



你是如何得到这个想法的? iostream是多态的。




为了使一个类具有多态性,它必须具有虚拟的

函数。 std lib中的iostream没有。因此,它们不是多态的。


如果您对此表示怀疑,那就做我做的事情:在
$ b $上调用write() b来自指向iostream类型的指针的stringstream,看看会发生什么。

上面的代码看起来更像是这个逻辑上无效的代码:

ostringstream os;
ostream * ptr =& os;

ptr-> write(这是一个测试。,strlen(这是一个测试。));



同样,这是一个错误的假设。是什么让你认为一个类
模板不能有虚函数?




它出现在通用编程一书中。或模板

元编程我第一次看到这个;我相信前者,因为它是b $ b是政策背后推理的一部分。我的编译器测试

会产生错误。还讨论了comp.lang.c ++。moderated

,标题为为什么模板函数不能是虚函数?。我的b $ b会粘贴一个链接,但是复制粘贴决定不起作用我会支持
现在可能需要重启我的电脑。你应该能够找到它自己的标题。


Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...

解决方案


ro**********@gmail.com wrote:

Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...



I realized what the problem was. I was using iostream as a polymorphic
entity and it isn''t. Code above would have looked more like this
logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don''t have virtual
functions and streams are templates. I was being stupid again.


ro**********@gmail.com wrote:

ro**********@gmail.com wrote:

Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...
I realized what the problem was. I was using iostream as a polymorphic
entity and it isn''t.



How did you get that idea? iostreams are polymorphic.
Code above would have looked more like this logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don''t have virtual
functions and streams are templates.



Again, this is a false assumption. What makes you think that a class
template cannot have virtual functions?



Rolf Magnus wrote:

ro**********@gmail.com wrote:

ro**********@gmail.com wrote:

Using ostringstream the buffer gets increased to allow data to be
appended when needed at least when you call <<. I was under the
impression that write() also did but I am having problems that make me
doubt that. Can I depend on the behavior of something like this?

ostringstream os;

os.write("This is a test.", strlen("This is a test."));
cout << os.str() << endl;

If so my problem is obviously caused by something else...
I realized what the problem was. I was using iostream as a polymorphic
entity and it isn''t.



How did you get that idea? iostreams are polymorphic.



In order for a class to be polymorphic it has to have virtual
functions. The iostreams in the std lib do not. They are therefore
not polymorphic.

If you doubt this then just do what I did: Call write() on a
stringstream from a pointer to type iostream and see what happens.

Code above would have looked more like this logically invalid code:

ostringstream os;
ostream * ptr = &os;

ptr->write("This is a test.", strlen("This is a test."));

This of course does not work because templates don''t have virtual
functions and streams are templates.



Again, this is a false assumption. What makes you think that a class
template cannot have virtual functions?



It was either in the book "Generic Programming" or "Template
Metaprogramming" that I first read this; I believe in the former as it
is part of the reasoning behind policies. Tests with my compiler
generate errors. There is also a discussion on comp.lang.c++.moderated
entitled "Why a template function cannot be a virtual function?". I
would paste a link but copy paste has decided not to work and I will
probably now have to reboot my computer. You should be able to find it
yourself with the title.


这篇关于ostringstream和write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆