CObject::IsKindOf
BOOL IsKindOf(
const CRuntimeClass* pClass
) const;
This function tests pClass to see if (1) it is an object of the specified class or (2) it is an object of a class derived from the specified class. This function works only for classes declared with the DECLARE_DYNAMIC, DECLARE_DYNCREATE, or DECLARE_SERIAL macro.
Do not use this function extensively because it defeats the C++ polymorphism feature. Use virtual functions instead.
If you have derived your class from CObject and used the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC, the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE, or the DECLARE_SERIAL and IMPLEMENT_SERIAL macros explained in the article Deriving a Class from CObject, the CObject class has the ability to determine the exact class of an object at run time.
This ability is most useful when extra type checking of function arguments is needed and when you must write special-purpose code based on the class of an object. However, this practice is not usually recommended because it duplicates the functionality of virtual functions.
The CObject member function IsKindOf can be used to determine if a particular object belongs to a specified class or if it is derived from a specific class. The argument to IsKindOf is a CRuntimeClass object, which you can get using the RUNTIME_CLASS macro with the name of the class.
To use the RUNTIME_CLASS macro
Use RUNTIME_CLASS with the name of the class, as shown here for the class CObject:
Copy Code
CRuntimeClass* pClass = RUNTIME_CLASS( CObject );
You will rarely need to access the run-time class object directly. A more common use is to pass the run-time class object to the IsKindOf function, as shown in the next procedure. The IsKindOf function tests an object to see if it belongs to a particular class.
To use the IsKindOf function
Make sure the class has run-time class support. That is, the class must have been derived directly or indirectly from CObject and used the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC, the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE, or the DECLARE_SERIAL and IMPLEMENT_SERIAL macros explained in the article Deriving a Class from CObject.
Call the IsKindOf member function for objects of that class, using the RUNTIME_CLASS macro to generate the CRuntimeClass argument, as shown here:
Copy Code
// in .H file
class CPerson : public CObject
{
DECLARE_DYNAMIC( CPerson )
public:
CPerson(){};
// other declaration
};
// in .CPP file
IMPLEMENT_DYNAMIC( CPerson, CObject )
void SomeFunction(void)
{
CObject* pMyObject = new CPerson;
if(pMyObject->IsKindOf( RUNTIME_CLASS( CPerson ) ) )
{
//if IsKindOf is true, then cast is all right
CPerson* pmyPerson = (CPerson*) pMyObject ;
...
delete pmyPerson;
}
...
delete [MyObject];
}
Note
IsKindOf returns TRUE if the object is a member of the specified class or of a class derived from the specified class. IsKindOf does not support multiple inheritance or virtual base classes, although you can use multiple inheritance for your derived Microsoft Foundation classes if necessary.
One use of run-time class information is in the dynamic creation of objects. This process is discussed in the article Dynamic Object Creation.
For more detailed information on serialization and run-time class information, see the articles Files in MFC and Serialization.