std::basic_streambuf::overflow
性病:基本[医]流光::溢出
virtual int_type overflow( int_type ch = Traits::eof() | | |
---|
确保在放置面积保存一些字符的初始子序列,从pbase()
到输出序列并更新指向PUT区域%28的指针(如果需要的话)为%29。如果ch
不是Traits::eof()
%28i.e.Traits::eq_int_type(ch, Traits::eof()) != true
%29,它要么放到PUT区域,要么直接保存到输出序列中。
该功能可能更新pptr
,,,epptr
和pbase
用于定义写入更多数据的位置的指针。如果失败,该函数将确保pptr() == nullptr
或pptr() == epptr
...
函数的基类版本什么也不做。派生类可以重写此函数,以便在耗尽时允许更新PUT区域。
参数
ch | - | the character to store in the put area |
---|
返回值
返回不等于Traits::eof()
在成功的时候,Traits::eof()
在失败的时候。
函数的基类版本返回Traits::eof()
...
注
大sputc()和sputn()在溢出%28的情况下调用此函数pptr() == nullptr或pptr() >= epptr()29%。
例
二次
#include <iostream>
#include <array>
// Buffer for std::ostream implemented by std::array
template<std::size_t SIZE, class CharT = char>
class ArrayedStreamBuffer : public std::basic_streambuf<CharT> {
public:
using Base = std::basic_streambuf<CharT>;
using char_type = typename Base::char_type;
using int_type = typename Base::int_type;
ArrayedStreamBuffer() : buffer_{} // value-initialize buffer_ to all zeroes
{
Base::setp(buffer_.begin(), buffer_.end() // set std::basic_streambuf
// put area pointers to work with 'buffer_'
}
int_type overflow(int_type ch)
{
std::cout << "overflow\n";
return Base::overflow(ch
}
void print_buffer()
{
for (const auto& i: buffer_) {
if (i == 0) {
std::cout << "NULL";
} else {
std::cout << i;
}
std::cout << " ";
}
std::cout << "\n";
}
private:
std::array<char_type, SIZE> buffer_;
};
int main()
{
ArrayedStreamBuffer<10> streambuf;
std::ostream stream(&streambuf
stream << "hello";
streambuf.print_buffer(
if (stream.good()) {
std::cout << "stream is good\n";
}
stream << "world";
streambuf.print_buffer(
if (stream.good()) {
std::cout << "stream is good\n";
}
stream << "!";
streambuf.print_buffer(
if (!stream.good()) {
std::cout << "stream is not good\n";
}
}
二次
产出:
二次
h e l l o NULL NULL NULL NULL NULL
stream is good
h e l l o w o r l d
stream is good
overflow
h e l l o w o r l d
stream is not good
二次
另见
uflow virtual | reads characters from the associated input sequence to the get area and advances the next pointer (virtual protected member function) |
---|---|
underflow virtual | reads characters from the associated input sequence to the get area (virtual protected member function) |
overflow virtual | writes characters to the associated file from the put area (virtual protected member function of std::basic_filebuf) |
overflow virtual | appends a character to the output sequence (virtual protected member function of std::basic_stringbuf) |
overflow virtual | appends a character to the output sequence, may reallocate or initially allocate the buffer if dynamic and not frozen (virtual protected member function of std::strstreambuf) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。