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
output.close();
// create a ifstream for now to read in
car C,D;
ifstream input("OutIn.txt");
input >> C >> D;
cout << C <<D << endl;
input.close();
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. |