C++
  Home arrow C++ arrow Page 3 - 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++ - The Insertion


    (Page 3 of 4 )

    The insertion operation is completed by the >> operator. It is a smart move from the designing team to overload the bitwise shift operator for types where you cannot do this, indeed where this does not make any sense. Reusability is once again scheduled.

    The first task we need to do is define the function of the operator overload like this:


    ostream& operator<< (ostream& out, const car& per);


    This is overloading the operator << (in fact it defines the operator) for types where, on the left side of the operator is an ostream, and on the right side a car type. The const assures the input class that through this process the data of the "per" will not change, and the reference passing of the argument is for performance purposes, as we avoid creating a new object and copying the data from the input inside it.

    Now all we need to do is perform the insertion of the class inside the function into the ostream. Nevertheless, here we have a small design issue. We need to access all of the data in the "car" class inside the function, as we might or might not have functions that guarantee access to its private or protected members.

    Here for instance we have the getName() and getID() public methods for this, but designing a class that will not divulge a member that we still want to serialize is surely an achievable task. Besides calling, a function can decrease the performance of your program. We need to make sure that we have access to all of the members of the class, so the road we need to pass on is the friend keyword.


    friend ostream& operator<< (ostream& out, const car& per);


    Applying the upper declaration of the function inside the car class will point out to the compiler that this function is a friend with the class and guarantee access to all non-public members/functions, just as if those were public. Now we can define the function outside the class in the following manner:


    ostream& operator<< (ostream& out, const car& per)

    {

    out << per.m_carType << 't';

    out << per.m_ID << endl;

    return out;

    }


    Inside the function, we just pass each build in type C into the stream, and you might want to make sure that the members are separated by some white spaces (or else you will see a long sequence of chars in your streams). Also make sure to call a flush at the end of your streaming (here the endl has encapsulated that option); otherwise, they might just be pushed into the buffer and, in the end, not displayed properly.

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek