CreateObject confused
I need some direction as to what the CreateObject function is actually
used for. I was reading about it and I got the impression at first that
one could create an object instance dynamically with CRuntime data
(kinda like wrapping some more stuff in with the new call ). Ok that
is cool ( if my understanding is even correct so far ? )
But I have a CMapStringToString object that is declared without
"new" or "CreateObject" but just as a member of MyDocClass.
But I noticed when stepping thru my serialize framework inside
of the
CObject* CArchive::ReadObject(const CRuntimeClass* pClassRefRequested)
function when reading in my archive from file, I am confused as to why
a new object has to be created at the call
// allocate a new object based on the class just acquired
pOb = pClassRef->CreateObject();
when ReadClass has already found the class written, and my program
has the CmStS object already created to recieve the read from my
serialize function ? Is it making a temp copy or something ? Also I
never really saw where the pOb was being deleted (although I may
have missed it ) but I'm assuming that MFC is taking care of that
somewhere ?
If you need to see my code for the CmStS and serialize it's below.
--------------------------------------------------
class CFileHandlingDoc : public CDocument
{
protected: // create from serialization only
CFileHandlingDoc();
DECLARE_DYNCREATE(CFileHandlingDoc)
// Attributes
public:
CMapStringToString ExpMap1; // is serializable
CMapStringToString* pExpMap1;
.......
}
--------------------------------------------------
In the view class data gets put in the map, here
I have just plugged in some recognizable stuff,
(code crunched up to save posting space)
CFileHandlingDoc* DocPtr = GetDocument();
CString One, Two, Three;
One = _T("WWW"); Two = _T("ZZZZ"); Three = _T("DDDD");
DocPtr->ExpMap1.SetAt(_T("a"), One);
DocPtr->ExpMap1.SetAt(_T("b"), Two);
DocPtr->ExpMap1.SetAt(_T("c"), Three);
//so my map now has some stuff in it.
----------And the doc serialize function------------------
void CFileHandlingDoc::Serialize(CArchive& ar)
{
pExpMap1 = &ExpMap1;
if (ar.IsStoring())
{
ar << pExpMap1;
}
else
{
ar >> pExpMap1;
}
}