C++
  Home arrow C++ arrow Page 2 - Serialize Your Class into Streams in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Serialize Your Class into Streams in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2009-04-21

    Table of Contents:
  • Serialize Your Class into Streams in C++
  • Why Should You Make Your Class Compatible with Streams?
  • The Insertion
  • The Extraction

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek