Re: A design problem associated with STL streams
On Mar 5, 2:21 pm, Steven Woody <narkewo...@gmail.com> wrote:
Supposing I have three types: class CA, CB and CC, and I need to
implement input/output of these types agains STL ostream/istream. I
know the ussual method would be overloading >> and << operators on
these types, but my problem is even harder. CA, CB, CC each has five
forms of representation ( each has five valid storage format ). So,
how should I resolve this problem in STL? By defining five different
istream/ostream derivations?
Certainly not.
Or use a single pair of istream/ostream with help of five
manipulators?
More likely.
A lot depends on context, but basically, you have two options:
define manipulators (using ios::xalloc() and ios::pword or
ios::iword), or use the decorator pattern. Which is preferred
depends on the semantics of the object; I've used both on
different occasions.
And as Victor pointed out, it's generally better if you can
automatically distinguish the representation on input, rather
than requiring the manipulator/decorator (and running the risk
of a mismatch).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34