名稱空間
變體
操作

std::strstreambuf::overflow

來自 cppreference.com
< cpp‎ | io‎ | strstreambuf
protected:
virtual int_type overflow( int_type c = EOF );
(C++98 起棄用)
(C++26 中移除)

將字元 c 新增到緩衝區的放置區,如果可能則重新分配。

1) 如果 c == EOF,則不執行任何操作。
2) 否則,如果放置區有可用的寫入位置 (pptr() < epptr()),則儲存字元,如同透過 *pptr()++ = c 儲存。
3) 否則,如果流緩衝區模式不是動態的,或者流緩衝區當前被凍結,則函式失敗並返回 EOF
4) 否則,函式會重新分配(或初始分配)一個足夠大的動態陣列,以容納當前動態陣列(如果有)的內容加上至少一個額外的寫入位置。如果在建構函式中使用了指向分配函式 palloc 的指標,則會呼叫該函式 (*palloc)(n),其中 n 是要分配的位元組數,否則使用 new char[n]。如果在建構函式中使用了指向釋放函式 pfree 的指標,則會呼叫該函式 (*pfree)(p) 來釋放先前的陣列(如果需要),否則使用 delete[] p。如果分配失敗,函式失敗並返回 EOF

目錄

[編輯] 引數

c - - 要儲存在放置區的字元

[編輯] 返回值

如果 c == EOF,返回除 EOF 之外的某個值。否則,成功時返回 (unsigned char)(c),失敗時返回 EOF

[編輯] 示例

#include <iostream>
#include <strstream>
 
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; // read-write dynamic 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

[編輯] 參閱

[virtual]
從放置區向關聯輸出序列寫入字元
(std::basic_streambuf<CharT,Traits> 的虛保護成員函式) [編輯]
[virtual]
將一個字元附加到輸出序列
(std::basic_stringbuf<CharT,Traits,Allocator> 的虛保護成員函式) [編輯]
[virtual]
從放置區向關聯檔案寫入字元
(std::basic_filebuf<CharT,Traits> 的虛保護成員函式) [編輯]
將一個字元寫入放置區並推進下一個指標
(std::basic_streambuf<CharT,Traits> 的公有成員函式) [編輯]
插入一個字元
(std::basic_ostream<CharT,Traits> 的公有成員函式) [編輯]