On Thursday 07 March 2002 13:10, you wrote:
> Do I not remember André telling me that code like this is very bad
> practise:
>
> ostringstream word;
> word << "some string";
> ...
> word.str("");
>
> because resetting the the pointers inside the ostringstream isn't
> guaranteed? Instead he suggested using a new ostringstream in such cases.

I presume that the supposed functionality of word.str("") is to reset the 
word to an empty one? Well, in my own code I usually do this, and AFAIK it is 
portable and clean:

ostrstream word;
word << "some string";
...
word << ends; // Finalize, and this is needed AFAIK
/** use word.str() any way you want */
...
word.rdbuf()->freeze(0);
word.seekp(0);
/** now word is *not* empty, but ready for writing */
/** if you want word that is BOTH empty and ready, add the lines below */
word << ends;
word.seekp(0);

I haven't tried that on ostringstream, but I presume it should work. All of 
that might be easiest to implement as

class ostrhelp {
        static void clrstr(ostringstream & sstr) {  
                sstr.rdbuf()->freeze(0); 
                sstr.seekpos(0); 
                sstr << ends; 
                sstr.seekpos(0); 
        }
}

And use as:

ostringstream word;
while (true) {
        word << "some string";
        word << ends;
        /** use word.str() in any way */
        strhelp::clrstr(word);
}

Cheers, Kuba

Reply via email to