description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | |||||
---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CRuntimeClass Structure |
CRuntimeClass Structure |
11/04/2016 |
|
|
de62b6ef-90d4-420f-8c70-f58b36976a2b |
Each class derived from CObject
is associated with a CRuntimeClass
structure that you can use to obtain information about an object or its base class at run time.
struct CRuntimeClass
Name | Description |
---|---|
CRuntimeClass::CreateObject | Creates an object during run time. |
CRuntimeClass::FromName | Creates an object during run time using the familiar class name. |
CRuntimeClass::IsDerivedFrom | Determines if the class is derived from the specified class. |
Name | Description |
---|---|
CRuntimeClass::m_lpszClassName | The name of the class. |
CRuntimeClass::m_nObjectSize | The size of the object in bytes. |
CRuntimeClass::m_pBaseClass | A pointer to the CRuntimeClass structure of the base class. |
CRuntimeClass::m_pfnCreateObject | A pointer to the function that dynamically creates the object. |
CRuntimeClass::m_pfnGetBaseClass | Returns the CRuntimeClass structure (only available when dynamically linked). |
CRuntimeClass::m_wSchema | The schema number of the class. |
CRuntimeClass
is a structure and therefore does not have a base class.
The ability to determine the class of an object at run time is useful when extra type checking of function arguments is needed, or when you must write special-purpose code based on the class of an object. Run-time class information is not supported directly by the C++ language.
CRuntimeClass
provides information on the related C++ object, such as a pointer to the CRuntimeClass
of the base class and the ASCII class name of the related class. This structure also implements various functions that can be used to dynamically create objects, specifying the type of object by using a familiar name, and determining if the related class is derived from a specific class.
For more information on using CRuntimeClass
, see the article Accessing Run-Time Class Information.
CRuntimeClass
Header: afx.h
Call this function to dynamically create the specified class during run time.
CObject* CreateObject();
static CObject* PASCAL CreateObject(LPCSTR lpszClassName);
static CObject* PASCAL CreateObject(LPCWSTR lpszClassName);
lpszClassName
The familiar name of the class to be created.
A pointer to the newly created object, or NULL if the class name is not found or there is insufficient memory to create the object.
Classes derived from CObject
can support dynamic creation, which is the ability to create an object of a specified class at run time. Document, view, and frame classes, for example, should support dynamic creation. For more information on dynamic creation and the CreateObject
member, see CObject Class and CObject Class: Specifying Levels of Functionality.
See the example for IsDerivedFrom.
Call this function to retrieve the CRuntimeClass
structure associated with the familiar name.
static CRuntimeClass* PASCAL FromName(LPCSTR lpszClassName);
static CRuntimeClass* PASCAL FromName(LPCWSTR lpszClassName);
lpszClassName
The familiar name of a class derived from CObject
.
A pointer to a CRuntimeClass
object, corresponding to the name as passed in lpszClassName. The function returns NULL if no matching class name was found.
[!code-cppNVC_MFCCObjectSample#17]
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
.
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.
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.
[!code-cppNVC_MFCCObjectSample#18]
A null-terminated string containing the ASCII class name.
This name can be used to create an instance of the class using the FromName
member function.
See the example for IsDerivedFrom.
The size of the object, in bytes.
If the object has data members that point to allocated memory, the size of that memory is not included.
See the example for IsDerivedFrom.
If your application statically links to MFC, this data member contains a pointer to the CRuntimeClass
structure of the base class.
If your application dynamically links to the MFC library, see m_pfnGetBaseClass.
See the example for IsDerivedFrom.
A function pointer to the default constructor that creates an object of your class.
This pointer is only valid if the class supports dynamic creation; otherwise, the function returns NULL.
If your application uses the MFC library as a shared DLL, this data member points to a function that returns the CRuntimeClass
structure of the base class.
If your application statically links to the MFC library, see m_pBaseClass.
See the example for IsDerivedFrom.
The schema number ( -1 for nonserializable classes).
For more information on schema numbers, see the IMPLEMENT_SERIAL macro.
See the example for IsDerivedFrom.
Hierarchy Chart
CObject::GetRuntimeClass
CObject::IsKindOf
RUNTIME_CLASS
IMPLEMENT_DYNAMIC
IMPLEMENT_DYNCREATE
IMPLEMENT_SERIAL