8

メソッドpubsetbufを使用して、文字列をコピーせずに文字列ストリームオブジェクトの文字列バッファを変更しようとしていますが、機能していません。http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/のドキュメントに従っています。これが私のサンプルコードです:

#include <iostream>
#include <sstream>

int main(int argc, char* argv[])
{
    std::stringstream stream("You say goodbye");
    char replace[] = {"And I say hello"};
    std::cout << stream.str() << std::endl; // Checking original contents
    stream.rdbuf()->pubsetbuf(replace, 16); // Should set contents here
    std::cout << stream.str() << std::endl; // But don't :(
    return 0;
}

そして、出力は次のとおりです。

You say goodbye
You say goodbye

stream.str(replace)を使用できることはわかっていますが、このメソッドは'replace'の値をコピーするため、コピーを作成したくありません。

私は何が欠けていますか?

更新:VS2010を使用しています

4

1 に答える 1

11

内容を設定しないでください。pubsetbuf呼び出しvirtual setbuf

basic_streambuf<charT,traits>* setbuf(charT* s, streamsize n);

15効果:setbuf(0,0)が効果を持たないことを除いて、実装定義。

16戻り値:this。

VS2010。に仮想メソッドのオーバーロードはありません。setbufデフォルトbasic_stringbufを使用します。basic_streambuf

virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
    {   // offer buffer to external agent (do nothing)
    return (this);
    }
于 2012-09-18T16:55:19.480 に答える