Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serializing an Array. Possible?

Status
Not open for further replies.

nyjil

Programmer
Jul 14, 2000
32
US
How do I serialize an array? I know it is possible to serialize int, CStrings, and other such variable forms. However, what if I have an hypothetical object, lets call it a CCat object. This object is made from 3 integer variables, and a CString variable. How do I serialize the whole object? Now the real question comes, how do I serialize an array of CCat objects?

What I'm looking to do is to save to disk and then later draw from that disk some information. Serialized data, so to speak. Not too hard. I have done it many times with common variables, but never a whole object. What makes it tough for me is the array. Further complicating matters is that my array is a two-dimensional array (i.e.
CCat[25][10]). In otherwords, I have 25 CCats across and 10 rows of CCats.

Any help on this matter would be greatly appriciated. Happy Holidays.

Nyjils-)
 
If you know to serialize an object, so what you need to do is:

1st, serialize the number of objects you have, then serialize object after object in a for (){} routine.

for reading, do the same, read the number of objects, then make a for from 0 to the number of objects you read -1, and read object by object.

Daniel Cohen Gindi
dcgsmix@gmx.net
 
Thank you both for your advise. A simple 'for' statement should do the trick. Next question. How do you serialize an object?
 
Hi

You add the function Serialize to your object class

void CMsg::Serialize(CArchive& ar)
{

if (ar.IsStoring())
{
ar << m_lAppId;
}
else
{
long l;

ar >> l;
m_lAppId = ( int) l;
}

HTH

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top