Dave's Brain

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);

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.