Serialization of CObjects

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;
};
nexguyAsked:
Who is Participating?
 
Answers2000Connect With a Mentor Commented:
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
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.