?
Solved

Serialization of CObjects

Posted on 1998-10-17
1
Medium Priority
?
381 Views
Last Modified: 2013-11-20
I have a CObLists of CObjects called Leagues.  Each leage has a CObList of other CObjects called teams.  And each one of those objects has a CObList of CObjects called players.  How can I implement serialization so that the whole tree of cobjects is stored into a file?  Then how do I re-read that data back into memory?  Finally, I know I must used macros in the class declarations to specify that they can be serialized. How is this done?
  Thankyou.

class  Player : public CObject{
//stuff
};
class Team : public CObject{
//stuff
   CObList PLAYERS;
};
class  League : public CObject{
//stuff
   CObList TEAMS;
};
0
Comment
Question by:nexguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 400 total points
ID: 1323487
Firstly (you probably know this bit).  To serialize a CObject you need these steps:

(begin quote)
1. Deriving your class from CObject (or from some class derived from CObject).
2. Overriding the Serialize member function.
3. Using the DECLARE_SERIAL macro in the class declaration.
4. Defining a constructor that takes no arguments.
5. Using the IMPLEMENT_SERIAL macro in the implementation file for your class.
(end quote)

Okay here are the specifics for you:
A. Make Player Serialize its data (you know how to do this right) with the above steps.

B. Make Team Serializable - do all normal steps.  For overloaded Serialize function do this
i. Call base class
ii. Check the archive variable IsLoading() member (to tell if loading or saving)
iii. If saving (flag is false)
- write out number of items:
ar << PLAYERS.GetCount() ;
- then step through the list using GetHeadPosition/GetNext and archive each player using "ar <<"
iv. If loading (flag is true)
- clear the obj list
- get the number of items
int nCount ;
ar >> nCount ;
- then loop nCount times, each time, use ar >> tempPlauyer and add this to the list

C. Make League Serializable - do all normal steps.  For overloaded Serialize function do this
i. Call base class
ii. Check the archive variable IsLoading() member (to tell if loading or saving)
iii. If saving (flag is false)
- write out number of items:
ar << TEAMS.GetCount() ;
- then step through the list using GetHeadPosition/GetNext and archive each team
iv. If loading (flag is true)
- clear the obj list
- get the number of items
int nCount ;
ar >> nCount ;
- then loop nCount times, each time, use ar >> tempTeam and this to the list

D. To serialize the whole thing, use the League Serialize functions.

Each of above steps is separate stage in development so you can compile and test each step individually before moving on, OR if really brave go for the whole thing in one go!

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question