• 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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eadhasAuthor Commented:
Edited text of question.
eadhasAuthor Commented:
Edited text of question.
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?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

eadhasAuthor Commented:
hi ernest,
give me ur code
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.