Browse - Programming Tips - How can I do run-time type checking with MFC?

Date: 2009sep29
Language: C++
Framework: MFC
Keywords: RTTI (Run Time Type Identification)
OS: Windows

Q.  How can I do run-time type checking with MFC?

A.  If your class inherits from CObject (which all the Microsoft MFC classes do) you can use the IsKindOf() method like this:

if (pMyObject->IsKindOf(RUNTIME_CLASS( CMyClass) ) ); { MessageBox(..., "Yes, its the right class"); }
Or
ASSERT(pMyObject->IsKindOf(RUNTIME_CLASS( CMyClass) ) );
There is even a build in assert for this:
ASSERT_KINDOF(CMyClass, pMyObject);