Dave's Brain

Browse - programming tips - mfc run time type checking

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

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);
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: