From: RB on
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;
}
}


From: Scott McPhillips [MVP] on
"RB" <NoMail(a)NoSpam> wrote in message
news:ukMESGLBLHA.1888(a)TK2MSFTNGP05.phx.gbl...
>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;
> }
> }
>
>

The CreateObject function is used to create an object whose type is not
known at compile time. For example, the MFC framework (which was compiled
in 2008 without knowing your class name) creates your CFileHandlingDoc at
startup, but it does so without a

CFileHandlingDoc* p = new CFileHandlingDoc();

statement. The CRuntimeClass provides the needed data about the object.

The same ability is used by CArchive to create objects from the file stream.
Instead of having a 'new' statement for each possible class it uses
CreateObject to create all objects.

--
Scott McPhillips [VC++ MVP]

From: RB on

>........................ For example, the MFC framework (which was compiled
> in 2008 without knowing your class name) creates your CFileHandlingDoc at startup, but it does so without a
> CFileHandlingDoc* p = new CFileHandlingDoc();
> statement. The CRuntimeClass provides the needed data about the object.
> The same ability is used by CArchive to create objects from the file stream. Instead of having a 'new' statement for each possible
> class it uses CreateObject to create all objects. Scott McPhillips [VC++ MVP]

Ok, that verbalizes more clearly, but if I may be so curious why does
my CMapStringToString Object which is already in existence need
CreateObject to create a object to receive my serialized read ?
It appears the WriteClass and ReadClass (called by WriteObject or
ReadObject ) has no problem fetching the CmStS's Cruntime data
from my file. So after that, I don't see why another object needs created
before actually reading the CMap in ?


From: Scott McPhillips [MVP] on
"RB" <NoMail(a)NoSpam> wrote in message
news:eJDD4lLBLHA.1972(a)TK2MSFTNGP02.phx.gbl...
>
>>........................ For example, the MFC framework (which was
>>compiled
>> in 2008 without knowing your class name) creates your CFileHandlingDoc at
>> startup, but it does so without a
>> CFileHandlingDoc* p = new CFileHandlingDoc();
>> statement. The CRuntimeClass provides the needed data about the object.
>> The same ability is used by CArchive to create objects from the file
>> stream. Instead of having a 'new' statement for each possible class it
>> uses CreateObject to create all objects. Scott McPhillips [VC++ MVP]
>
> Ok, that verbalizes more clearly, but if I may be so curious why does
> my CMapStringToString Object which is already in existence need
> CreateObject to create a object to receive my serialized read ?
> It appears the WriteClass and ReadClass (called by WriteObject or
> ReadObject ) has no problem fetching the CmStS's Cruntime data
> from my file. So after that, I don't see why another object needs created
> before actually reading the CMap in ?


I have not delved into this deserialize code, but I suspect the answer is
that CArchive is highly generalized code that does not know or care about
your CmStS member. It just makes the map object, then lets your >>
statement take care of where it goes (via a copy c'tor). That would be the
way to code it generically.

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
See previous explanation. It is because there is a completely uniform way to create
objects during serialization input, and the input reader doesn't have to figure out if the
object was known when MFC was built or is a user-specific invention that came later.
joe

On Sat, 5 Jun 2010 10:14:41 -0400, "RB" <NoMail(a)NoSpam> wrote:

>
>>........................ For example, the MFC framework (which was compiled
>> in 2008 without knowing your class name) creates your CFileHandlingDoc at startup, but it does so without a
>> CFileHandlingDoc* p = new CFileHandlingDoc();
>> statement. The CRuntimeClass provides the needed data about the object.
>> The same ability is used by CArchive to create objects from the file stream. Instead of having a 'new' statement for each possible
>> class it uses CreateObject to create all objects. Scott McPhillips [VC++ MVP]
>
>Ok, that verbalizes more clearly, but if I may be so curious why does
>my CMapStringToString Object which is already in existence need
>CreateObject to create a object to receive my serialized read ?
>It appears the WriteClass and ReadClass (called by WriteObject or
>ReadObject ) has no problem fetching the CmStS's Cruntime data
>from my file. So after that, I don't see why another object needs created
>before actually reading the CMap in ?
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm