共用方式為


CRuntimeClass::IsDerivedFrom

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;

Parameters

  • 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.

Remarks

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.

Note

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.

Example

// 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"),
      pClass->m_lpszClassName);
   return FALSE;
}

// Get a pointer to the base class CRuntimeClass.
#ifdef _AFXDLL
   CRuntimeClass* pBaseClass = pClass->m_pfnGetBaseClass();
#else
   CRuntimeClass* pBaseClass = pClass->m_pBaseClass;
#endif
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, 
   pClass->m_wSchema);

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

Requirements

Header: afx.h

See Also

Reference

CRuntimeClass Structure

Hierarchy Chart