In a real MFC program, an instance of the CRuntimeClass structure replaces the static s_lpszClassName data member shown above. This structure has data members for the class name and the object size; it also contains a pointer to a special static function, CreateObject, that's supposed to be implemented in the target class. Here's a simplified version of CRuntimeClass:
struct CRuntimeClass { char m_lpszClassName[21]; int m_nObjectSize; // used for memory validation CObject* (*m_pfnCreateObject)(); CObject* CreateObject(); };
The real MFC CRuntimeClass structure has additional data members and functions that navigate through the class's hierarchy. This navigation feature is not supported by the official C++ RTTI implementation.
This structure supports not only class name retrieval but also dynamic creation. Each class you derive from CObject has a static CRuntimeClass data member, provided that you use the MFC DECLARE_DYNAMIC, DECLARE_DYNCREATE, or DECLARE_SERIAL macro in the declaration and the corresponding IMPLEMENT macro in the implementation file. The name of the static data member is, by convention, class<class_name>. If your class were named CMyClass, the CRuntimeClass data member would be named classCMyClass.
If you want a pointer to a class's static CRuntimeClass object, you use the MFC RUNTIME_CLASS macro, defined as follows:
#define RUNTIME_CLASS(class_name) (&class_name::class##class_name)
Here's how you use the macro to get the name string from a class name:
ASSERT(RUNTIME_CLASS(CMyClass)->m_lpszClassName == "CMyClass");
If you want the class name string from an object, you call the virtual CObject::GetRuntimeClass function. The function simply returns a pointer to the class's static CRuntimeClass object, just as earlier the GetClassName function returned the name string. Here's the function you'd write for CMyClass:
virtual CRuntimeClass* GetRuntimeClass() const { return &classCMyClass; }
And here's how you'd call it:
ASSERT(pMyObject->GetRuntimeClass()->m_lpszClassName == "CMyClass");