Declare_dynamic/implement_dynamic macro

Source: Internet
Author: User

Original article: http://blog.chinaunix.net/uid-26881496-id-3175819.html

Eclare_dynamic/implement_dynamic macro
Some friends recently got stuck by several Macros in Chapter 3 when reading "let's get down to MFC". I remember these macros stuck when I first read this book. Of course, the real compaction is actually the first one, that is, declare_dynamic/implement_dynamic. I made a detailed explanation for the reference of friends who are also stuck :)

Note: The main purpose of these two macros is to add some static member functions and static member variables to the declaration and implementation of the specified class (such as cview. Therefore, if you don't care about the line feed mark "\", you can regard every sentence in it as a declaration or implementation in the class. In addition, I will not repeat the "#" and "#" symbols.
In addition, it is recommended that jjhou write some macro-expanding instances in conjunction with the book "let alone MFC". It may be better to look at this interpretation with examples :)

Declare_dynamic/implement_dynamic

# Define declare_dynamic (class_name )\
Public :\
Static cruntimeclass class ## class_name ;\
// Declare a static public member variable of the cruntimeclass type. The variable name is a string named "class"
// It is composed of the specified class name. For example, if you write declare_dynamic (cmyview ),
// Static cruntimeclass classcmyview static variable

Virtual cruntimeclass * getruntimeclass () const ;\
// Declare a virtual function named getruntimeclass. The returned value is a pointer of the cruntimeclass type.
// No parameters and a const Function

# Define implement_dynamic (class_name, bass_class_name )\
_ Implement_runtimeclass (class_name, base_class_name, 0 xFFFF, null)

# DEFINE _ implement_runtimeclass (class_name, base_class_name, wschema, pfnnew )\
Static char _ lpsz # class_name [] = # class_name ;\
// Define a static variable of the C-type string. The variable name consists of "_ lpsz" and the class name of the specified class. The variable value is the name of the specified type.
// For example, cmyview, static char _ lpszcmyview = "cmyview" is defined ";

Cruntimeclass class_name: class # class_name = {\
_ Lpsz # class_name, sizeof (class_name), wschema, pfnnew ,\
Runtime_class (base_class_name), null };\
// Assign a value to the static member variable of the cruntimeclass type defined in declare_dynamic.
// Of course, except for the last m_pnextclass, no value is assigned (the value is null, which is processed by the following structure)

Static afx_classinit _ init _ # class_name (& class_name: Class ## class_name );\
// Initialize a static afx_classinit structure named "_ init _ # class_name". It mainly serves
// Class ## assign values to the last member of the class_name static variable m_pnextclass. For details, see the following description in afx_classinit.

Cruntimeclass * class_name: getruntimeclass

() Const \
{Return & class_name: class # class_name ;}\
// The implementation of getruntimeclass defined in declare_dynamic is very simple, just a return statement.

# Define runtime_class (class_name )\
(& Class_name: class # class_name)
// The reason for this part is to define a macro separately, mainly to help you directly obtain its cruntimeclass static member from a specified class.

// The following describes the afx_classinit structure. Note that this is not a macro. You can refer to the following content after replying to this post.

The hidden content of this post must be replied before browsing.

[This post was last edited by becoming a master at on]

When learning the documentation, views, and frameworks of MFC, you must know the class declaration of the derived classes of these three classes.
Add declare_dyncreate, and then add implement_dyncrea to an appropriate place outside the class declaration.
Te, and then the documents, views, frameworks, and document templates can be used to coordinate the work. Check msdn and find
Similar macros have these pairs:
Declare_dynamic and implement_dynamic
Declare_dyncreate and implement_dyncreate
Declare_serial and implement_serial

Although their roles are described in msdn, they do not know why they play such a role.
So I flipped through the source code of MFC. People who like to drill corners can come with me for a drill.

1.
The runtime_class macro is defined as follows:
# Define runtime_class (class_name)
(Cruntimeclass *) (& class_name: Class ## class_name ))
# Indicates macro extension of the symbols on both sides (if they are macros), and then Extension
The following content is connected together without spaces in the middle. For example, runtime_class (cview) will be extended
:
(Cruntimeclass *) (& cview: classcview)
But what does classcview mean? Originally, classcview was created by declare_dynamic (cview)
A static member variable of the cruntimeclass type introduced in the public attribute:
Static const afx_data cruntimeclass classcview;

In the past, runtime_class was used to reference the static member changes introduced by the declare_dynamic macro.
Quantity.

2. Declare_dynamic (class_name)
Due to the length, the specific definition code of the macro will not be listed. If you are interested, you can view the file.
Afx. h.
The macro declares three members to the class:
Protected:
Static cruntimeclass * Pascal _ getbaseclass ();
Public:
Virtual cruntimeclass * getruntimeclass () const;
Static const afx_data cruntimeclass class # class_name;

There are two member functions, a static member variable class + class name, similar to runtime_class, such
If declare_dynamic (cview) is used, the static member variable will be classcview. This
Member variable names are related to the declare_dynamic parameters. Next we will change this Member
All data is recorded as class ## class_name.

Where is the static member and two member functions initialized and implemented? Originally in impl
Ement_dynamic macro.

3. Implement_dynamic (class_name, base_class_name)
View its macro definition. If _ afxdll is defined
The initialization and implementation of members are as follows:

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 ),
0 xFFFF,
Null,
Null,
& Class_name: _ getbaseclass,
Null
}; // This is the static member variable class ## class_name during initialization.
// For the meaning of each member of the cruntimeclass structure, see msdn.

4. _ Declare_dynamic (class_name)
The definition of this macro is basically the same as that of declare_dynamic (class_name. The difference is that static members
Class ## no const modifier before class_name.

5. Declare_dyncreate (class_name)

This macro also introduces the three Members introduced by the declare_dynamic macro to the class. In addition, it
Another member is introduced:
Static cobject * Pascal Createobject ();
The Members introduced by this macro are initialized and implemented in implement_dyncreate.

6. Implement_dyncreate (class_name, base_class_name)
This macro is naturally a member of initialization and implementation introduced by declare_dyncreate.
Let's take a look at the implementation of Createobject:
Cobject * Pascal class_name: Createobject ()
{
{
Return new class_name;
}
Oh, this function is so simple, it is to create
Objects of this type.

7. _ Declare_dyncreate (class_name)
This macro introduces approximately four Members introduced by declare_dyncreate. Unique difference
Is the static member class # class_name introduced by the macro without the const modifier.

8. Declare_serial (class_name)
This macro introduces four identical ones as introduced by _ declare_dyncreate.

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.