Explore Runtime

zhaozj2021-02-16  43

Explore the secrets such as runtime_class

When you learn MFC to learn documents, views, and frameworks, you must add Declare_DyncReate in the class declaration of these three classic class, then add import_dyncrea tete, then document, view, and framework, There is a document template to coordinate work. View msdn, find similar macros, these pairs: Declare_Dynamic and import_dynamic declare_dyncreate and import_dyncreate declare_serial and import_serial Although their role is introduced in MSDN, but why do they have such a role in my heart, so I turn over MFC? Source code, people who like to drill the horn can diamond with me. 1. The definition of the runtime_class macro is like this: #define ruventime_class (CRUNTIMECLASS *) (& class_name :: clas ## class_name)) Where ## means to macro expansion ## sides of the symbol (if they are macro The words, then connect the extended content together, there is no space in the middle. For example: runtime_class (cView) will be extended: (CRUNTIMECLASS *) (& CView :: ClasscView) But what does this classcview mean? The original, classCView CRuntimeClass is a type of static member variables public property introduced by DECLARE_DYNAMIC (CView): static const AFX_DATA CRuntimeClass classCView; RUNTIME_CLASS original role is a reference to a static member variables introduced by DECLARE_DYNAMIC macro. 2. Due to the reason, the specific definition code of the macro is not listed because of the reasons for the space, and I can see the file AFX.h. The macro to declare the three members of the class: protected: static CRuntimeClass * PASCAL _GetBaseClass (); public: virtual CRuntimeClass * GetRuntimeClass () const; static const AFX_DATA CRuntimeClass class ## class_name; two member functions, a static member variables Class class name, similar to runtime_class, if it is declare_dynamic (cView), this static member variable will be ClasscView. It can be seen that the name of this member variable is related to the parameters of Declare_Dynamic. Here we made this member to change the quantitative Class ## Class_name. This static member and two member functions are initialized and concrete? It turned out to be in IMPL Ement_Dynamic Hongri. 3.

IMPLEMENT_DYNAMIC (class_name, base_class_name) to view its macro definition, if _AFXDLL been defined, then the initialization and realize introduced by DECLARE_DYNAMIC members is this: CRuntimeClass * PASCAL class_name :: _ GetBaseClass () {return RUNTIME_CLASS (base_class_name); } CRuntimeClass * class_name :: GetRuntimeClass () const {return RUNTIME_CLASS (class_name);} AFX_COMDAT const AFX_DATADEF CRuntimeClass class_name :: class ## class_name = {#class_name, sizeof (class class_name), 0xFFFF, NULL, NULL, & class_name :: _ GetBaseClass Null}; // This is in initializing static member variables Class ## Class_name. The meaning of each member of the // CruntimeClass structure can be viewed for MSDN. 4. _Declare_dynamic (class_name) This macro definition is basically the same as Declare_Dynamic (class_name). The difference is that the static member class ## Class_name does not have a Const modifier in front of it. 5. Declare_DyncReate (class_name) This macro also introduces three members introduced by the Declare_Dynamic macro. In addition, it also introduces a member: static cobject * pascal createObject (); the member introduced by the macro is initialized and implemented in IMPLEMENT_DYNCREATE. 6. Implement_dyncreate (class_name, base_class_name) This macro is naturally an initialization and implementation of members introduced by Declare_Dyncreate. Let's take a look at CreateObject: cobject * pascal class_name :: createObject () {{return new class_name;} Oh, this function is so simple, it is an object to create a type of type using the new operator overloaded in the COBJECT class. . 7. _Declare_dyncreate (class_name) This macro introduces a member of the four members introduced with Declare_Dyncreate. The only difference is that the static member of the macro introduced Class ## Class_name does not have a const modifier. 8. Declare_serial (class_name) This macro introduced the same four members introduced by _Declare_dyncreate, and it also has more this: AFX_API Friend Carchive & Afxapi Operator >> (CARCHIVE & Ar, Class_name * & Pob); It turned out to be overloaded The operator Operator >> is used as the friend of this class. So you can access the members of the class in the operator function Oper Ator >>. 9.

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

New Post(0)