| re: A question about istringstream
JustSomeGuy wrote:[color=blue]
> Jonathan Mcdougall wrote:
>
>[color=green][color=darkred]
>>>I am passing an istringstream to a function. I want that function to
>>>get a copy of the istringstream and not a refrence to it.[/color]
>>
>>streams can't be copied.
>>
>>[color=darkred]
>>>ie when the function returns I want the istringstream to be
>>>unmodified...[/color]
>>
>>make it a const reference.
>>
>>[color=darkred]
>>>However when I try to pass it
>>>
>>>fn(istringstream s) // doesn't compile
>>>but
>>>fn(istringstream & s) // does.
>>>
>>>What should I do to?[/color]
>>
>>fn(const istringstream &s)
>>
>>If you really need to work on a copy, do it manually (but the parameter
>>will always have to be a const reference because you can't pass streams
>>by value).
>>
>>void fn(const std::istringstream &iss);
>>
>>int main()
>>{
>> std::istringstream iss;
>>
>> // fill 'iss'
>>
>> std::istringstream copy_of_iss;
>> copy_of_iss.str(iss.str());
>>
>> fn(copy_of_iss);
>>}
>>
>>So fn() works on a reference to a copy.
>>[/color]
>
>
> This sounds like a good idea.. but when you make a copy of the iss stream[/color]
I don't, I defined a new object using the same string as its buffer.
[color=blue]
> does the current 'stream buffer point' get preserved..[/color]
Of course not, nor its state or whatever data it has, nothing got
copied. I just made a new istringstream and set its buffer to the same
string as the first one.
Particularily, to set the buffer's read position, just do something like
copy_of_iss.seekg(iss.tellg());
[color=blue]
> I guess the bottom line is that the operator= hasn't been defined for this
> stream class...[/color]
The bottom line is: streams are not copyable by design.
Jonathan |