The detailed explanation of Declare_dynamic/implement_dynamic macro in the Book of "in-depth MFC" __MFC

Source: Internet
Author: User
Recently some friends in the "Simple and Simple MFC", by the third chapter of a few macros to get stuck, remember the first time I read this book, also by these several macros to get stuck. Of course, the real card is actually the first one, that is, declare_dynamic/implement_dynamic. I made a detailed explanation for the same stuck friend to make a reference:

Description: The main purpose of these two macros is to add static member functions and static member variables to the Declaration and implementation of the specified class (such as CView). So, instead of "/" the line sign, take every word inside as a declaration in a class or an implementation. In addition, the "# #" and "#" symbol I will not have to repeat.
In addition, the proposed combination of "Easy to MFC" This book, Jjhou wrote some examples of expanded macros, combined with the example to see this explanation may be better effect:

Declare_dynamic/implement_dynamic

#define DECLARE_DYNAMIC (class_name)/
public:/
static CRuntimeClass class# #class_name;
Declares a static public member variable of type CRuntimeClass, and the variable name is by the string "class"
Consists of the class name of the specified class. For example, if you write Declare_dynamic (CMyView), it is tantamount to declaring a
static CRuntimeClass classcmyview statically variable

Virtual cruntimeclass* getruntimeclass () const;/
Declares a virtual function named Getruntimeclass, and returns a pointer with a value of CRuntimeClass type
No arguments, and is a const function

#define IMPLEMENT_DYNAMIC (Class_name,bass_class_name)/
_implement_runtimeclass (Class_name,base_class_name,0xffff,null)

#define _IMPLEMENT_RUNTIMECLASS (class_name,base_class_name,wschema,pfnnew)/
static char _lpsz# #class_name []= #class_name;
Defines a C-type string static variable whose name consists of "_lpsz" and the class name of the specified class, with the value of the variable being the name of the specified type
For example is CMyView, then the definition is static char _lpszcmyview= "CMyView";

CRuntimeClass class_name::class# #class_name = {/
_lpsz# #class_name, sizeof (class_name), wschema,pfnnew,/
Runtime_class (base_class_name), null};/
Assign values to static member variables of the type of CRuntimeClass that were previously defined in Declare_dynamic
Of course, there is no assignment except the last M_pnextclass (Assignment is null, it is handled by the following structure)

Static Afx_classinit _init_# #class_name (&class_name::class# #class_name);
Initializes a afx_classinit static structure named "_init_# #class_name", which is primarily for the specified class_name
class# #class_name静态变量的最后一个成员m_pNextClass赋值, see below to explain afx_classinit in detail

cruntimeclass* Class_name::getruntimeclass () const/
{return &class_name::class# #class_name;} /
The implementation of the Getruntimeclass previously defined in declare_dynamic, very simply, is a return statement.

#define RUNTIME_CLASS (class_name)/
(&class_name::class# #class_name)
This part of the reason for define a macro, mainly for the convenience of a specified class directly to get its CRuntimeClass static members

The following is an explanation of the afx_classinit structure, note that this is not a macro
To see more clearly, I wrote the struct definition in the usual format defined by struct.
struct Afx_classinit {
Afx_classinit (CRuntimeClass *pnewclass);
};

Afx_classinit::afx_classinit (CRuntimeClass *pnewclass)
{
Pnewclass->m_pnextclass = CRuntimeClass::p firstclass;
Let M_pnextclass point to the CRuntimeClass variable that the Pfirstclass refers to

CRuntimeClass::p firstclass = Pnewclass;
Let Pfirstclass point to the variable that Pnewclass refers to, which is the CRuntimeClass static variable of this class
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.