Of course, you need to write the copy constructor itself and the copy assignment operator.
Then you need to decide what kind of semantics you need a copy. So:
TestStream test; TestStream test2; test2 << "foo" test = test2; test << "bar"; test2.str.str(); // should this be "foo" or "foobar" ?
If you need a shallow copy, ( "foobar" ), then you need to split the stringstream object between multiple instances of TestStream , perhaps shared_ptr best used for this.
If you need a deep copy ( "foo" ), you can copy like this:
TestStream(const TestStream &rhs) : str(rhs.str.str()) {}
Or use one of the options in the question you are referring to.
This covers the line to which you are in the middle of the letter when you take a copy. If you are in the middle of reading from it or if you are writing, but you cannot write to the end due to the use of seekp , then you need to fix the current read / write positions, as well as the data in the string stream that you execute with tellg/tellp .
You can also copy content in stream format, etc., which is what copyfmt does, and even error flags ( rdstate - copyfmt leave them alone).
Steve jessop
source share