Solved

Serialization of CObjects

Posted on 1998-10-17
1
379 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change owner from userA to server Local Administrators 7 73
difference between String.subString() and String.subSequence() 6 285
bigDiff challenge 17 142
Example code 13 36
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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