std::strstreambuf::overflow

< cpp‎ | io‎ | strstreambuf
protected:
virtual int_type overflow (int_type c = EOF);

后附字符 c 到缓冲的放置区,若可能则重分配。

1)c==EOF ,则不做任何事
2) 否则,若放置区拥有可用的写位置( pptr() < epptr() ),则如同用 *pptr()++ = c 存储该字符
3) 否则,若流缓冲模式非动态,或流当前为冻结,则函数失败并返回 EOF
4) 否则,函数重分配(或初始分配)足够大以保有当前动态数组(若存在)加至少一个额外写入位置的动态数组。若构造函数中使用了指向分配函数的指针 palloc ,则以 (*palloc)(n) 调用该函数,否则用 new char[n] ,其中 n 为要分配的字节数。若构造函数中使用了指向解分配函数的指针 pfree ,则以 (*pfree)(p) 调用该函数分配先前的数组,否则用 delete[] p ,若需要解分配。若分配失败,则函数失败并返回 EOF

参数

c - 要存储于放置区的字符

返回值

c==EOF ,则返回异于 EOF 的某值。否则,成功时返回 (unsigned char)(c) ,失败时返回 EOF

示例

#include <strstream>
#include <iostream>
 
struct mybuf : std::strstreambuf
{
    int_type overflow(int_type c) 
    {
        std::cout << "Before overflow(): size of the put area is " << epptr()-pbase()
                  << " with " << epptr()-pptr() << " write positions available\n";
        int_type rc = std::strstreambuf::overflow(c);
        std::cout << "After overflow(): size of the put area is " << epptr()-pbase()
                  << " with " << epptr()-pptr() << " write positions available\n";
        return rc;
    }
};
 
int main()
{
    mybuf sbuf; // 读写动态 strstreambuf
    std::iostream stream(&sbuf);
 
    stream << "Sufficiently long string to overflow the initial allocation, at least "
           << " on some systems.";
}

可能的输出:

Before overflow(): size of the put area is 16 with 0 write positions available
After overflow(): size of the put area is 32 with 15 write positions available
Before overflow(): size of the put area is 32 with 0 write positions available
After overflow(): size of the put area is 64 with 31 write positions available
Before overflow(): size of the put area is 64 with 0 write positions available
After overflow(): size of the put area is 128 with 63 write positions available

参阅

从放置区写入字符到关联的输出序列
(std::basic_streambuf<CharT,Traits> 的虚受保护成员函数)
后附字符到输出序列
(std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数)
从放置区写字符到关联的文件
(std::basic_filebuf<CharT,Traits> 的虚受保护成员函数)
写一个字符到放置区域,并令 next 指针前进
(std::basic_streambuf<CharT,Traits> 的公开成员函数)
插入字符
(std::basic_ostream<CharT,Traits> 的公开成员函数)