Call this function to determine if the calling class is derived from the class specified in the pBaseClass parameter.

BOOL IsDerivedFrom( 
   const CRuntimeClass* pBaseClass  
) const;


  • pBaseClass
    The familiar name of a class derived from CObject.

Return Value

TRUE if the class calling IsDerivedFrom is derived from the base class whose CRuntimeClass structure is given as a parameter; otherwise FALSE.


The relationship is determined by "walking" from the member's class up the chain of derived classes all the way to the top. This function only returns FALSE if no match is found for the base class.


To use the CRuntimeClass structure, you must include the IMPLEMENT_DYNAMIC, IMPLEMENT_DYNCREATE, or IMPLEMENT_SERIAL macro in the implementation of the class for which you want to retrieve run-time object information.

For more information on using CRuntimeClass, see the article CObject Class: Accessing Run-Time Class Information.


// This example creates an object from the run-time class. It only  
// creates objects derived from CWnd. 

// We only want to create an object derived from CWnd. 
if (!pClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)))
   TRACE(_T("Error; Object %s is not derived from CWnd\n"),
   return FALSE;

// Get a pointer to the base class CRuntimeClass.
#ifdef _AFXDLL
   CRuntimeClass* pBaseClass = pClass->m_pfnGetBaseClass();
   CRuntimeClass* pBaseClass = pClass->m_pBaseClass;
ASSERT(pBaseClass != NULL);

TRACE("Creating object %s derived from %s, with object size %d " 
   "and schema %d\n", pClass->m_lpszClassName, 
   pBaseClass->m_lpszClassName, pClass->m_nObjectSize, 

// Create the object.
CObject* pObject = pClass->CreateObject();


Header: afx.h

See Also


CRuntimeClass Structure

Hierarchy Chart