Scratching my head (extraction operator)

Hi guys,

Im attempting to write(overload) an extraction operator for a class. It works fine and the fail bit is setting correctly when there is an error in the stream.

My problem is that a requirement of this operator is to leave the stream unaltered if an error in the stream is detected. That means that I can't touch the stream (ie can't put characters back into the stream etc).

I was thinking that it is possible to make a copy of the stream and manipulate it that way, and if no errors are present then I can flush the stream at the end. The only problem with this is that I have no idea how to copy the stream without actually flushing it. If anyone can help me out it would be greatly appreciated.

Thanks.
Last edited on
You can't copy streams. The copy constructors are private.

Use get() while saving the return values in a stack until you encounter an error or succeed. If you encounter an error, putback() the contents of the stack into the stream.
Is there a good guide to doing this? I'm not 100% confident with stacks.
Uh... Huh?
1
2
3
4
5
6
7
std::stack<int> stack;
for (int a=0;a<10;a++)
    stack.push(a);
while (!stack.empty()){
    std::cout <<stack.back();
    stack.pop();
}
Topic archived. No new replies allowed.