Click here to Skip to main content
16,012,166 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Running at set times Pin
David Crow23-May-05 2:46
David Crow23-May-05 2:46 
GeneralInvalid handle issue for a Service application while using GenerateConsoleCtrlEvent Pin
jiju_m_jiju21-May-05 19:39
sussjiju_m_jiju21-May-05 19:39 
GeneralPrint preview Pin
Anonymous21-May-05 15:54
Anonymous21-May-05 15:54 
GeneralDevice contexts Pin
Anonymous21-May-05 15:43
Anonymous21-May-05 15:43 
GeneralRe: Device contexts Pin
Blake V. Miller21-May-05 18:20
Blake V. Miller21-May-05 18:20 
GeneralDynamic splliter windows Pin
Anonymous21-May-05 15:01
Anonymous21-May-05 15:01 
GeneralSerialization Pin
Anonymous21-May-05 15:00
Anonymous21-May-05 15:00 
GeneralRe: Serialization Pin
Tom Archer21-May-05 15:13
Tom Archer21-May-05 15:13 
*** WARNING ***
I whipped up an example in a few minutes so the code is far from tight. However, it should server as a guide to illustrate how to serialize a linked list of objects from an MFC application.

It would be the same as serializing any object in that the document would iterate the linked list and serialize each of the objects.

For example:

* Let's say you have a class called CTest that has a next pointer to support a linked list of CTest objects:
class CTest : public CObject
{
DECLARE_SERIAL(CTest)
public:
	CTest(CString strValue);
	CTest();	
public:
	CString m_strValue;
	CTest* m_pNext;
public:
	void Serialize(CArchive& ar);
};


* You'll note that it's responsible for serializing itself - just as any object. You could do this serialization from the documents Serialize or from any other function that makes sense for your app.

* The CTest class' implementation might look something like this where it is initialized via being serialized:
IMPLEMENT_SERIAL(CTest, CObject, 1)
CTest::CTest()
{
	m_strValue = _T("-1"); // invalid
	m_pNext = NULL;
}

CTest::CTest(CString strValue)
{ 
	m_strValue = strValue;
	m_pNext = NULL;
}
void CTest::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << m_strValue;
		ar << m_pNext;
	}
	else
	{
		ar >> m_strValue;
		ar >> m_pNext;
	}
}


* As an example of using the document's Serialize (since that's the specific question you had):

void CLinkedListDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ASSERT(m_pHead);
		if (m_pHead)
		{
			for (CTest* pCurr = m_pHead; pCurr != NULL; pCurr = pCurr->m_pNext)
			{
				pCurr->Serialize(ar);
			}
		}
	}
	else
	{
		m_pHead = new CTest();
		m_pHead->Serialize(ar);
		
		CTest* pCurr = m_pHead;
		while (pCurr->m_pNext)
		{
			CTest* pNext = new CTest();
			pNext->Serialize(ar);
			
			pCurr->m_pNext = pNext;
			pCurr = pNext;
		}
	}
}


* Several things to note here
* When storing, note how I simply walk the linked list and call the object's Serialize function for each object
* When loading, note how I use the stored CTest::m_pNext member only to tell me that another CTest object is in the file. That way I know to instantiate a blank CTest and then call CTest::Serialize to read in its values until I reach a CTest object that has a m_pNext value of NULL.



Cheers,
Tom Archer - Archer Consulting Group

"So look up ahead at times to come, despair is not for us. We have a world and more to see, while this remains behind." - James N. Rowe
GeneralRe: Serialization Pin
Ravi Bhavnani22-May-05 5:50
professionalRavi Bhavnani22-May-05 5:50 
GeneralRe: Serialization Pin
David Crow23-May-05 2:48
David Crow23-May-05 2:48 
GeneralRestoring Multiple Toolbars Pin
Ron Ritchie21-May-05 12:58
Ron Ritchie21-May-05 12:58 
GeneralRe: Restoring Multiple Toolbars Pin
Jack Puppy21-May-05 20:44
Jack Puppy21-May-05 20:44 
GeneralRe: Restoring Multiple Toolbars Pin
Ron Ritchie25-May-05 11:52
Ron Ritchie25-May-05 11:52 
GeneralRun in memory Pin
Dennis L21-May-05 11:50
Dennis L21-May-05 11:50 
GeneralRe: Run in memory Pin
Alexander M.,21-May-05 12:55
Alexander M.,21-May-05 12:55 
GeneralRe: Run in memory Pin
autodebug21-May-05 17:20
autodebug21-May-05 17:20 
GeneralWindow Hook overhead Pin
peterchen21-May-05 10:48
peterchen21-May-05 10:48 
Generalturning images Pin
SuperTank21-May-05 8:22
SuperTank21-May-05 8:22 
Generalname of the node Pin
rgchezhian21-May-05 7:05
rgchezhian21-May-05 7:05 
GeneralRe: name of the node Pin
Prakash Nadar21-May-05 19:15
Prakash Nadar21-May-05 19:15 
GeneralRe: name of the node Pin
rgchezhian21-May-05 21:03
rgchezhian21-May-05 21:03 
Generalnotification Pin
Anonymous21-May-05 5:21
Anonymous21-May-05 5:21 
GeneralRe: notification Pin
Ravi Bhavnani21-May-05 6:01
professionalRavi Bhavnani21-May-05 6:01 
GeneralRe: notification Pin
Tom Archer21-May-05 7:22
Tom Archer21-May-05 7:22 
Generalin Win32 (SDK)color After Printing getting too much light Pin
chachva21-May-05 5:05
chachva21-May-05 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.