CRuntimeClass and Class Name problem 
Author Message
 CRuntimeClass and Class Name problem

I want to find a way to serialize an instance of any CObject derived so
that it can be deserialized later.

What I am trying to do is to serialize the runtime class of the object,
before serializing the object itself, so that when deserializing I can get
the runtime class of the object and call its contructor dynamically by
calling CRuntimeClass::CreateObject ().

My idea was to serialize the object's m_lpszClassName data member and pass
this to the RUNTIME_CLASS macro to get a pointer to a CRuntimeClass, and so
call CreateObject ().  But m_lpszClassName is a character array while
RUNTIME_CLASS requires an actual class name.

Any ideas how to serialize the class name so that I can retrieve a
CRuntimeClass pointer?



Mon, 31 Jul 2000 03:00:00 GMT  
 CRuntimeClass and Class Name problem

I'm not sure if this is what you want, but anyway........
When you load from the archive, you do not need to know the object class:

When storing the object do it like

a << pMyClass;

Now when you load from the archive

CFile f(_T("myfile.dat"), CFile::modeRead);
CArchive a(&f, CArchive::load);
CObject* pObject;
 a >> pObject;

TRACE("Object class = %s\n", pObject->GetRuntimeClass()->m_lpszClassName);

// NOTE: You should insert som error-checking !!!!!

----------------------

James W. Minett skrev i meddelelsen

Quote:
>I want to find a way to serialize an instance of any CObject derived so
>that it can be deserialized later.

>What I am trying to do is to serialize the runtime class of the object,
>before serializing the object itself, so that when deserializing I can get
>the runtime class of the object and call its contructor dynamically by
>calling CRuntimeClass::CreateObject ().

>My idea was to serialize the object's m_lpszClassName data member and pass
>this to the RUNTIME_CLASS macro to get a pointer to a CRuntimeClass, and so
>call CreateObject ().  But m_lpszClassName is a character array while
>RUNTIME_CLASS requires an actual class name.

>Any ideas how to serialize the class name so that I can retrieve a
>CRuntimeClass pointer?



Mon, 31 Jul 2000 03:00:00 GMT  
 CRuntimeClass and Class Name problem

This is done for you automatically by CArchive.

When you write your object using the overloaded << operator, you actually
generate

inline CArchive& operator<<(CArchive& ar, CMyObject* pOB)
{
    ar.WriteObject(pOB);
    return ar;

Quote:
}

CArchive::WriteObject function writes the runtime class data into the
archive.

When you read your object:

inline CArchive& operator>>(CArchive& ar, CMyObject* pOB)
{
    pOB = ar.ReadObject(RUNTIME_CLASS(CMyObject));
    return ar;

Quote:
}

CArchive::ReadObject reads a runtime class block off the archive and
compares it to the one you passed it.  If it does not match it will even
look to see if the passed class is derived from the one that was read.
After finding a match, it calls the runtime class member m_pfnCreateObject
which points to your class' static CreateObject member which returns a new
object of your class type.  It than calls that object's Serialize member.

This all happens automagically through MFC and the DECLARE_SERIAL and
IMPLEMENT_SERIAL macros.

--
VC++ 5.0sr3  WinNT 4.0sr3
Douglas Peterson
Software Engineer
Telex Communications, Inc
(For E-mail reply, remove the *'s)


Quote:
>I want to find a way to serialize an instance of any CObject derived so
>that it can be deserialized later.

>What I am trying to do is to serialize the runtime class of the object,
>before serializing the object itself, so that when deserializing I can get
>the runtime class of the object and call its contructor dynamically by
>calling CRuntimeClass::CreateObject ().

>My idea was to serialize the object's m_lpszClassName data member and pass
>this to the RUNTIME_CLASS macro to get a pointer to a CRuntimeClass, and so
>call CreateObject ().  But m_lpszClassName is a character array while
>RUNTIME_CLASS requires an actual class name.

>Any ideas how to serialize the class name so that I can retrieve a
>CRuntimeClass pointer?



Mon, 31 Jul 2000 03:00:00 GMT  
 CRuntimeClass and Class Name problem

John

Thanks for your help - as I often do, I made an easy job more complex by
using CRuntimeClass.

James Minett



Quote:
> I'm not sure if this is what you want, but anyway........
> When you load from the archive, you do not need to know the object class:

> When storing the object do it like

> a << pMyClass;

> Now when you load from the archive

> CFile f(_T("myfile.dat"), CFile::modeRead);
> CArchive a(&f, CArchive::load);
> CObject* pObject;
>  a >> pObject;

> TRACE("Object class = %s\n",

pObject->GetRuntimeClass()->m_lpszClassName);
Quote:

> // NOTE: You should insert som error-checking !!!!!

> ----------------------



Sun, 06 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. CRuntimeClass and real dynamic class creation

2. Creating Class from CRuntimeClass

3. Creating Class from CRuntimeClass

4. Trying to include classes from .dll, getting linker error with CRuntimeClass

5. class class-name: base-class : interface

6. MFC 7 - CRuntimeClass problem

7. Unresolved CRunTimeClass problems

8. How to set app name and class name

9. how to get a window's class name and window name

10. Nested class name lookup problem

11. Class name problem

12. Class Identification (Class Name)

 

 
Powered by phpBB® Forum Software