Solved

Serialization of CObjects

Posted on 1998-10-17
1
378 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
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 67
Complete beginner needs help making a cron job 9 125
How to know only "File created" with EventLog 3 35
Hibernate methods 2 77
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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