Home arrow C++ arrow Page 2 - Serialize Your Class into Streams in C++
C++

Serialize Your Class into Streams in C++


Writing code in C++ is not a trivial task, but quite manageable for anyone who gets into his or her head the desire to achieve success in the coding segment of life. To make the coding process more understandable, C++ offers the stream concept. This article will be all about how to make your class compatible with this technology.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
April 21, 2009
TABLE OF CONTENTS:
  1. · Serialize Your Class into Streams in C++
  2. · Why Should You Make Your Class Compatible with Streams?
  3. · The Insertion
  4. · The Extraction

print this article
SEARCH DEVARTICLES

Serialize Your Class into Streams in C++ - Why Should You Make Your Class Compatible with Streams?
(Page 2 of 4 )

Making your class compatible with the streaming function is in reality a straightforward task. You simply output anything that you need inside an overload of the insertion/extraction operator. Let me present  the advantage of implementing a streaming process first.

I have constructed a simple class that represents a car inside a database. As with any car at this level, it will have a unique ID and a name:


class car

{

 

public:

car(){};

car(string carType, int ID)

{m_carType = carType; m_ID = ID;}

virtual ~car() {};


string getName(){return m_carType;}

int getID(){return m_ID;}

void setCar(string carN)

{ m_carType = carN;}


private:

string m_carType;

int m_ID;

};


How do we serialize this object easily? We might just use the Iostream library of C++. Now I will present my examples in the cout (the console stream), but this can be generalized to any kind of stream.

Michael, for instance, assumes that he as a programmer is more interested in the ID, so apply a print like this:


car A(1, "Ford");

cout << A.getID() << 't' << A.getName();


Later, Joe enters the picture, and he believes that, for the user interface, the name is more important. So he decides to push the ID into the background the ID, and inverts the printing order:


car B(1, "Ford");

cout << B.getName()<< 't' <<B.getID();


Now imagine doing this inside a serialization program. Each time Joe tries to read in the data written by Michael (or vice versa), most probably undefined behavior will occur, because an inconsistency of the two serializations exists. Converting the name to a number and the number to a name is not a good idea at all.

Now all of this, placed inside of a single main function, may look trivial, but imagine this is inside a large application -- one that has tens of thousands of lines. Things will be much harder to synchronize once a group of programmers start working on this program, not just you. 

Besides, explicitly writing down every time how the members of the class should be printed is a time consuming activity -- one that will extend the length of the code you need to spend looking over the code, and increase the number of places where errors can occur. We should be able to directly apply the insertion/extraction operator to our class, and write all this just once in an obvious place for maintenance purposes.

The iostream members will accept any basic build types like char*, int, string, and so forth. All we have to do is make our class a basic build type as well, and we will accomplish this by defining the operator overload for it. Proceed to the next page to see how to do so.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials