Save - worked example
key : purple - original code / blue - new code
Step 1 :
Continue with the program names.
Step 2 :
Change the m_NameList definition as shown below (the pointer list must be of type CObList to allow saving) :
private:
// define the CTypedPtrList m_myList
CTypedPtrList<CObList, CName*> m_NameList;
Step 3 :
To allow the CName class to read and save data add the following definitions to the header (private access) :
DECLARE_SERIAL(CName);
void Serialize(CArchive& ar);
Step 4:
The following code should be added to Serialize() function in the CName class :
void
CName::Serialize(CArchive &ar)
{
CObject::Serialize( ar );
if (ar.IsStoring())
{ar << m_strFirstName << m_strSecondName;}
else
{ar >> m_strFirstName >> m_strSecondName;
}
}
IMPLEMENT_SERIAL( CName, CObject, 0 )
Step 5:
The following code should be added to Serialize() function in the Document :
void
CNamesDoc::Serialize(CArchive& ar)
{
m_NameList.Serialize(ar);
if (ar.IsStoring())
{// TODO: add storing code here
}
else
{m_nCurrentPosition = m_NameList.GetHeadPosition();
}
}
Click here for the basic exercise
Click here for the source code
Click here to return to menu