Serialize Your Class into Streams in C++
(Page 1 of 4 )
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.
So you started learning to code in C++. You know how to create a class, learned the golden rule that a "deconstructor" will always exist, and have decided that it's time to learn about streams. What a great tool to use if you know what it is all about.
I assume you know how to use streams for writing some data to a file and not only to the console. If this statement is false in your case, I advise you to read some of my other articles on this topic, published here on the Developer Shed Network. Once you understand that, then handling the serialization of objects with just a few lines will become manageable.
However, there is a problem. When you try to pass to the stream, your very own and custom class (with its own internal members) the compiler just puts out an error that this is an invalid move. The reason for this issue is that the streams are compatible by default only with the default types known by C++. If all this is true in your case, you are in a right place, as here I will show you how to resolve it.
This process, known also as insertion and extraction in streams, is an easy and practical way to resolve serialization. In the world of C++, I/O tasks are completed through the iostream library, which is an object-oriented class library that makes use of multiple and virtual inheritance. This is provided inside the "iostream" library as a component of C++.
Next: Why Should You Make Your Class Compatible with Streams? >>
More C++ Articles
More By Gabor Bernat