“内部"与“内部"之间的区别与“相关"流缓冲区 [英] Difference between "internal" vs "associated" stream buffer

查看:69
本文介绍了“内部"与“内部"之间的区别与“相关"流缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 http://www.cplusplus.com/reference/ios/ios/rdbuf/:

某些派生的流类(例如stringstream和fstream)维护它们自己的内部流缓冲区,它们在构造时与之关联.调用此函数以更改关联的流缓冲区对该那个内部流缓冲区无效:该流将具有一个关联的流缓冲区不同于其内部流缓冲区(尽管流的输入/输出操作始终使用此成员函数返回的关联的流缓冲区).

Some derived stream classes (such as stringstream and fstream) maintain their own internal stream buffer, to which they are associated on construction. Calling this function to change the associated stream buffer shall have no effect on that internal stream buffer: the stream will have an associated stream buffer which is different from its internal stream buffer (although input/output operations on streams always use the associated stream buffer, as returned by this member function).

并在 http://www.cplusplus.com/reference/fstream/ifstream/rdbuf/:

返回指向内部filebuf对象的指针.

但是请注意,这不一定与当前关联的流缓冲区(由ios :: rdbuf返回)相同.

Notice however, that this is not necessarily the same as the currently associated stream buffer (returned by ios::rdbuf).

那么,如果内部缓冲区不用于输入和输出操作,那么内部缓冲区又是什么呢?如果这意味着这两行可以返回两个不同的对象,那为什么有用呢?

So what is the internal buffer for if it's not used for input and output operations? And if it means that these two lines could return two different objects, why could this be useful?

std::stringstream ss;
ss.rdbuf();                          // (1) returns "internal" stream buffer?
static_cast<std::ios&>(ss).rdbuf();  // (2) returns "associated" stream buffer?

推荐答案

内部缓冲区用于输入和输出操作,直到使用其他自变量调用 rdbuf 为止.然后就坐在那里.

The internal buffer is used for input and output operations up until the point of calling rdbuf with some other argument. Then it just sits there.

您随时可以通过调用重新启用它

You can always re-enable it by calling

stream.basic_ios::rdbuf(stream.rdbuf());

请注意,例如 std :: fstream 始终是stream对象拥有和管理的 std :: filebuf 对象,而关联的缓冲区可以是派生的任何 streambuf 目的.该流仅存储指向它的基类指针,而不管理其生存期.

Note that the internal buffer for e.g. std::fstream is always an std::filebuf object owned and managed by the stream object, while the associated buffer can be any streambuf derived object. The stream only stores a base class pointer to it and doesn't manage its lifetime.

还要注意,术语内部缓冲器"是指内部缓冲器".未由标准使用.该标准使用了一些不同的术语:

Also note that the term "internal buffer" is not used by the standard. The standard uses a bit different terminology:

basic_ifstream< charT,traits> 支持读取命名文件.它使用 basic_filebuf< charT,traits> 对象控制关联的序列.为了说明起见,维护的数据在这里表示为:

The class basic_ifstream<charT, traits> supports reading from named files. It uses a basic_filebuf<charT, traits> object to control the associated sequence. For the sake of exposition, the maintained data is presented here as:

sb filebuf 对象.

维护的数据"以上就是cplusplus.com所谓的内部流缓冲区".

The "maintained data" above is what cplusplus.com calls "the internal stream buffer".

这篇关于“内部"与“内部"之间的区别与“相关"流缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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