• C

serialization in c++

can i serialize C++object like in JAVA.
if it can, please anyone give me the details how to do in C++.


Who is Participating?
Ernest022699Connect With a Mentor Commented:
In my VC++ 6.0 application I have Site Objects.  These are Objects which are collections of data for a specific location (like a building).  The most important thing to remember about serialization is the types of data which you can and cannot serialize.  Reading the online documentation will be essential!

Please note that this is real working code from a real application.  I assume here that you can open a file, etc.  It goes nearly unsaid that you may have additional questions for clarification.

First, the actual serialization:

void CSiteObject::m_fnSerialize(CArchive &ar)
      // Serialize the base object first ...

      // ... then our class-specific stuff.
      if (ar.IsStoring() )
            { // storing Site Object data
            ar << m_csSiteID;
            ar << m_csPropertyID;
            ar << m_csSiteDescription;
            ar << m_csFranchiseCode;

            } // end of storing Site Object data
            { // retrieving Site Object data
                        ar >> m_csSiteID;

                        ar >> m_csPropertyID;
                        ar >> m_csSiteDescription;
                        ar >> m_csFranchiseCode;
            } // end of retrieving Site Object data

} // end of CSiteObject::m_fnSerialize

Next, the calls to create the archive and call the above function:

      if (!myFile.Open(m_csArchiveFileName.operator LPCTSTR(),
                  CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive
                  ) )
            { // failed to open file
            MessageBox(NULL, "Failed to open archive file on Save.",
                  m_csArchiveFileName.operator LPCTSTR(),

            iRetVal = POLLER_SYSTEM_ERROR;
            } // end of failed to open file
            { // opened the file OK
            // create the Archive
            CArchive myArchive(&myFile, CArchive::store);

            // Next, do the actual serialization.


            } // end of opened the file OK

eadhasAuthor Commented:
Edited text of question.
eadhasAuthor Commented:
Edited text of question.
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

Not knowing JAVA, I can't be sure that C++ does things exactly the same way.  I can, however, give you working code for C++.  Is that what you need?
eadhasAuthor Commented:
hi ernest,
give me ur code
C++ has no built in method to serialize objects. There are libraries that give that functionality, as Ernest showed. You could also implement such functionality yourself.
Right you are, KangaRoo!  Before there were libraries (such as MFC) to use, we had to do everything (in C) ourselves.  Always a nightmare.

For me, Big Endian versus Little Endian was a problem when porting between an Intel-based UNIX platform and a Sun SPARC running Solaris.

It all comes down to wrappers.  The wise programmer hides storage and retrieval of data in a single module which can be rewritten as needed.  The display module never needs to know how the data were stored on disk....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.