Home arrow C++ arrow Page 4 - Serialize Your Class into Streams in 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
  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

Serialize Your Class into Streams in C++ - The Extraction
(Page 4 of 4 )

The insertion assumes that we take the same passes, with just a couple of differences. The direction of the operator changes; now you need to overload the operator >>. You need to declare/define the function for an ifstream type.

As long as you know how the input will look, your task is trivial. For example, for the upper class, I know that the order is name followed by the ID number, and all this is separated by white spaces (the newline and the tab). So I just read it in, maintaining the order of the output.

So declare the friend function inside the class:

friend istream& operator>> (istream& out, car& per);

Define the function as follows:

istream& operator>> (istream& in, car& per)


in >> per.m_carType;

in >> per.m_ID;

return in;


Here let's point out that the input argument of the class is no longer const, as we will modify its members with the input data. Returning the stream (here and at the insertion process) is required so that we can maintain the chain affect of the insertion/extraction process like this:

cin << "Yeti" << endl ;

Now that we have this entire task completed, let us see it in action:

// first construct a few items from the upper

car A("Ford", 1);

car B("Toyota", 2);

// create a ofstream into what will print it

ofstream output("OutIn.txt");


output << A <<B; // this will print all to the first person


// create a ifstream for now to read in

car C,D;

ifstream input("OutIn.txt");

input >> C >> D;


cout << C <<D << endl;


The output result is exactly what we were looking for:

Ford 1

Toyota 2

Press any key to continue . . .

Following the convention within the stream and taking the steps outlined above, instead of writing a function like writeToStream, will make your code easier to read. Now we have a centerpiece that we can easily maintain and make sure there is no conflict is between the input and the output.

Our type is now included between the basic build types, so if we want another class that encapsulates our car class inside its insertion and extraction function, we don't need to serialize our object any more; just call the operator on it, as we've already done here.

However, if you want to serialize your own data throughout your files in a system that has a more complicated build, I strongly recommend that you take a look at Boost's serialization classes. They offer you this support in a simple, efficient and portable way.

With this we've concluded this article, and I want to thank you for investing your time in reading it and invite you to post your ideas here on the blog or join our friendly, ever-growing forum over at DevHardware. Until we meet again: "Live with Passion!"

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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