There\'s a ton of information available on overloading operator<<
to mimic a toString()
-style method that converts a complex object to a string.
Some additional notes:
when implementing the operator>>, you probably should consider using the bufstream and not other overloads of operator>>;
exceptions occuring during the operation should be translated to the failbit or the badbit (members of streambuf may throw, depending on the class used);
setting the state may throw; if you set the state after catching an exception, you should propagate the original exception and not the one throwed by setstate;
the width is a field to which you should pay attention. If you are taking it into account, you should reset it to 0. If you are using other operator>> to do basic works, you have to compute the width you are passing from the one you received;
consider taking the locale into account.
Lange and Kreft (Standard C++ IOStreams and Locales) conver this in even more details. They give a template code for the error handling which takes about one page.