29.8. Serializing a CRuntimeClass

Here’s a special bit of MFC arcana that took quite a while to figure out! How do we serialize a CRuntimeClass ? Here’s an example of how to do it from force.cpp file. The long comment explains why this was a difficult thing to do.

 void cForceClassEvade::Serialize(CArchive &ar) { cForce::Serialize(ar); /* I had a hard time figuring out how to serialize the CRuntimeClass *_pnodeclass. (1) You can’t call _pnodeclass.Serialize(ar), as this is not a method of CRuntimeClass. (2) Nor can you save off the CRuntimeClass m_lpszClassName field and try and use RUNTIME_CLASS to reconstruct it, as RUNTIME_CLASS requires a literal argument and not a CString. (3) MFC provides a CArchive::SerializeClass(CRuntimeClass *prtc). ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.