Serializing and writing to a file

Started by
1 comment, last by Telastyn 19 years, 1 month ago
So, what's the difference between serializing and just having a 'read' and 'write' function for an object?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
I'd say the difference is when you deserialize something, you don't know beforehand what to read. For instance, if I were to give you a serialized object, you would have to figure out the real type of the object (not the base type), create that object and call its read function.

For instance:

class A {public:virtual void Read( istream );virtual void Write( ostream );};class B : public A {public:virtual void Read( istream );virtual void Write( ostream );};void main( ) {Serialize( new B( ), stream_out );A * Deserialize( stream_int );}Serialize calls Write( ) on its first argument. However, Deserialize must first create a correct object, and then call Read on it. This often goes hand in hand with a factory pattern and unique type identifiers, or having types as first-class objects (as in C#) and being able to call the constructor of an object given its type.
Serializing also doesn't always mean the stuff is going to file either.

The resultant serialized version of the object might also be sent across a network or displayed to the screen, or even sent to a printer. Having a standard serialize function means you don't have to re-write/copy that code into object->write() and object->send() and object->display() and object->laserjet().

This topic is closed to new replies.

Advertisement