About dynamic objects in MFC

xiaoxiao2021-03-06  14

About Objects in MFC Dynamics

In the MFC, the dynamic creation of the object also depends on the category records established by RTTI, and the Dynamic Create related CRUntimeClass members have two:

COBJECT * (PASCAL * m_pfncreateObject (); // point to functions used to create objects

COBJECT * CREATEOBJECT (); // Create an object using m_pfncreateObject

With the category recorded by RTTI, Declare_DyncReate / Implement_DyncReate continues to add dynamically created features to the class.

#define declare_dyncreate (Class_name) /

DECLARE_DYNAMIC (Class_name) /

Static COBJECT * PASCAL CREATEOBJECT ();

This macro is equally used in class declarations, not only adds Class ## Class_name objects for classes, but also adds a static function creteObject for classes to dynamically creation of objects. The same import_dyncreate is used to define and initialize the content added above.

#define import_dyncreate (class_name, base_class_name) /

COBJECT * PASCAL CLASS_NAME :: CreateObject () /

{RETURN New Class_name;} /

Implement_runtimeclass (class_name, base_class_name, 0xffff, /

Class_name :: CreateObject, NULL

Unlike the import_dynamic, the fourth parameter in the import_runtimeclass here is not NULL, but Class_name :: CreateObject, which specifies the function used to create an object, as for other content, nothing different from RTTI.

So, the dynamic creation process of the entire object is very simple, as long as we find a particular class of CruntimeClass objects, use class ## Class_name.createObject ().

COBJECT * CRUNTIMECLASS :: CreateObject ()

{

COBJECT * POBJECT = NULL;

POBJECT = (* m_pfncreateObject) ();

Return POBJECT;

}

Below is the entire process

Use Declare_Dynamic in class declaration

Use Implement_Dynamic in the class definition file, at this time

Class ## Class_name.m_pfncreateObject = Class_name :: CreateObject;

CruntimeClass :: CreateObject can be created with M_PFNCREATEOBJECT to create objects.

转载请注明原文地址:https://www.9cbs.com/read-51031.html

New Post(0)