• Advertisement
Sign in to follow this  

Serializing and writing to a file

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, what's the difference between serializing and just having a 'read' and 'write' function for an object?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement