Implementation principle of dynamic creation (dynamic creation) of MFC

Source: Internet
Author: User
Document directory
  • 1. Definition of cruntimeclass
  • 2. Macro declare_dynceate
  • 3. Macro implement_dyncreate
  • 4. Create an object

You may be confused about the document view architecture that uses MFC, that is, many classes are inexplicably instantiated without knowing where they are.

Take the single-document view as an example. In the code, you can see that there are only two examples of global variables of cwinapp, and the other is
Csingledoctemplate instantiation. Its constructor is as follows:

Csingledoctemplate (uint nidresource, cruntimeclass * pdocclass,

Cruntimeclass * pframeclass, cruntimeclass * pviewclass );

Then you will be confused about the next thing. First, the class inherited from singledoctemplate instantiates the class inherited from cframewnd and the class inherited from cdocument, then, the class inherited from cframewnd instantiates the class inherited from cview.

You may think of some code that has been written to instantiate these classes, but what puzzles you most is what I customize to inherit from cview, when cdocument and other classes are called, the name can be obtained by yourself. The code I have written can be unknown to the crowdsourced security testing team. Do you know this kind of name? Otherwise, I don't know how to instantiate a class name?

MFC naturally does not have such magical capabilities. So there is no doubt that the new class name you have defined will be uploaded to a certain place and then instantiated there. What is the mechanism behind it?

This is the so-called dynamic generation (dynamic creation) mechanism. This mechanism is useful to the rtti mentioned above and expands some functions based on rtti.

 

A simple example of dynamic creation 1. Definition of cruntimeclass

FirstIn the cruntimeclass definition, in addition to the rtti information, add

StructCruntimeclass{

Cobject * (* m_pfncreateobject) (); // function pointer. An object will be instantiated through the new function.

Cobject * Createobject ()

// Other information is omitted

}

The Createobject function is defined as follows:

Cobject * cruntimeclass: Createobject (){

Cobject * pobect = NULL;

Pobject = (* m_pfncreateobject )();

}

 

2. Macro declare_dynceate

Suppose we define a class cmydoc that inherits from cdocument.

Let's see csingledoctemplate * pTMP = new (... runtime_class (cmydoc)...) // ignore other parameters here.

How was cmydoc created? First look at the macro defined in cmydoc

 

/////// Cmydoc header file //////

Declare_dynceate (cmydoc)

//////////////////////////////////////// ///////

Equivalent:

Static cobject * Createobject ();

StaticCruntimeclass classcmydoc;

 

Macro definition:

# Define declare_dyncreate (class_name )\

Declare_dynamic (class_name )\

Static cobject * Createobject ();

 

3. Macro implement_dyncreate

///// // Cmydoc CPP file /////////////////

Implement_dyncreate (cmydoc, cdocument)

//////////////////////////////////////// /////////

In addition to the preceding rtti information, the preceding macro also has

Cobject * cmydoc: Createobject (){

Return new cmydoc;

}

Cmydoc: classcmydoc. m_pfncreateobject = cmydoc: Createobject;

 

Macro definition:

# Define implement_dyncreate (class_name, base_class_name )\

Cobject * class_name: Createobject ()\

{Return New class_name ;}\

// Implement_runtimeclass (...) // This is omitted for the convenience of discussion.

 

4. Create an object

With the two macros aboveThe csingledoctemplate constructor has the runtime_class (cmydoc) parameter ),We know

# Define runtime_class (class_name) (class_name: getthisclass ())

Is actually a pointer to static cruntimeclass classcmydoc in cmyview.

ThereforeYou can call the function to create an object.

Cobject * pdoc = runtime_class (cmydoc)-> Createobject ();// Actually called

Cobject * cmydoc: Createobject (){

Return new cmydoc;

}

 

 

Summary:

You may see a bunch of messy things above, and you just want to think about it. In the end, you don't just need the name of a class, and then use the name to instantiate a class. is it necessary to make it so troublesome?

This function is simple to say, but there are no other simple methods to implement it. Unless the C ++ syntax is changed, the compiler adds some additional support. We know what parameters can be passed in the function.

However, instantiating a class such as cview * view = new cview; // The name of a class cannot be used as a parameter.

For example, void Createobject (string classname ){

Classname * view = new classname;

}

Such functions cannot be compiled. however, what we mentioned above seems to be implemented in this way. because it is a macro, just a simple text replacement, the macro replacement step is not yet the turn of the compiler to work. after the macro is replaced, the compiler starts.

With macros, you can use the class name as a "parameter" to implement more than half of the functions. in fact, if you just use the previous two macros to aggregate a static function static cobject * Createobject (); you can. however, this is obviously not elegant enough, and since there is a cruntimeclass, it is better to simply bring the function for dynamically creating objects to this struct (cruntimeclass is used in many places, dynamic type identification, dynamic creation, serialization ).

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.