In C ++, the class name is used to create the corresponding class Object (rtti)

Source: Internet
Author: User
Download the program code:, think about the class name (a c ++ string), and create the corresponding class instance. In addition to this string, nothing else is needed. For example, I have declared a pclassstr class. Now I use the string "pclassstr" to create an instance of the pclassstr class. Google, most of the results are related to typeof and typeid, further c ++ 0x, and an auto.
Typeid is used to extract information of a C ++ class (or object) and obtain a typeinfo instance. Then, you can determine whether a class object is a Class Object Based on typeinfo, or whether two class objects belong to the same class. Type info class redefined = ,! = It completes the above functions and naturally defines the functions that can obtain the relevant information of this class. It cannot be used here.
The typeof keyword is only supported by the gnu c compiler. Specifically: int * A; typeof (* A) B; // Of course, B is of the int type, but it does not meet my needs. I only need to follow a string, you can create an object, as in Java. If typeof is used, it seems that I must have an instance of the class I want to create, just as I must have an int-Related Object A, that is, if I want to create an instance of the pclassstr class, I have to have an object of pclassstr, the typeof function is really important, but it is not powerful here.
Embedded template <typename T> class pclassstr {public: typedef t value_type ;//....} in general, just as the vector <pclassstr>: value_type in the vector is the same as the typeof above, you must first write the type of a class before jumping out of the Code, then write the variable name trap (such as type variable ).
In fact, the solution has been solved by Microsoft for a long time. When designing MFC, Microsoft developed it. Hou Jie's <simple introduction to MFC> has already analyzed this in depth. Here I will give a brief introduction and extract the relevant macro to implement a small program from the source code of MFC, the effect is acceptable ~
We know that there is a class hierarchy in MFC. cobject is a top-level class, and other classes inherit this class to form a class system. In fact, Java also uses this class architecture. We need to embed a static struct (cruntimeclass) in each defined class, record the class name, create the function pointer (Createobject) of the class object, and so on, these two fields are used. When creating each class, we will add the cruntimeclass of this class to a global linked list. Given the name of a class (a string), you only need to traverse the global linked list and find the cruntimeclass corresponding to this string (of course, the class name will not be repeated ), because this cruntimeclass contains a function pointer that is used to create objects of the corresponding class, this meets my requirements.
In my applet, all classes are inherited from pobject. The declaration of each class should contain the following similar code. You can write a macro definition:


Static struct cruntimeclass classpclassstr;

Static struct afx_classinit _ init_pclassstr;

Virtual struct cruntimeclass * getruntimeclass () const;

Static pobject * Createobject ();

The following similar code must be included in the class implementation:

Struct cruntimeclass pclassstr: classpclassstr = {

"Pclassstr ",

Sizeof (pclassstr ),

0 xFFFF,

Pclassstr: Createobject,

(& Pobject: classpobject ),

Null, & pclassstr: _ init_pclassstr };

Struct afx_classinit pclassstr: _ init_pclassstr (& (pclassstr: classpclassstr ));

Struct cruntimeclass * pclassstr: getruntimeclass () const


Return (& pclassstr: classpclassstr );


Pobject * pclassstr: Createobject ()


Return new pclassstr;


Pay attention to the usage of the afx_classinit struct and how to manage the global linked list.

For more information, see <For more information, see MFC>. For more information about macro definitions, see the source code of the MFC framework. For details about the global linked list header, see applet code.

The program does not manage the inheritance architecture of the class, but simply adds all the classes to the single-link table. It only implements my ideas. To implement the typeof function, there is still a lot to do.

-- EOF

Related Article

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: 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.