Programming Fundamentals--c++ can I support the reflection of Java and OBJC?

Source: Internet
Author: User
Tags reflection expression engine

Can C + + support the reflection of Java and OBJC?

To answer this question. First we need to know what reflection is. What is reflection?

I will not say the explanation of the textbook, (^o^) In fact, I do not remember. The reflection of the actual development application is to access the value of the member field of an object of this type without a header file of a type or a class structure definition. Call the member function (method) of this object.

For example, I have defined a type Class A with A,b,c three fields and a fun () function.

Now I have only one void* pA in my hand, note that its type is just a void pointer, and I don't have a class header file in my hand. How do I get it, A, B. What about the value of C? How do I invoke the fun function? Another requirement is to run the code through a string. For example, I have got a pA for a, and got a string "Pa->fun ()", and now how to do this string represents the code. Run it? (the most typical requirement for this latter requirement is the expression engine). The above is the use and advantages of reflection, very many practical needs are very very convenient, can save a lot of time in a lot of occasions, less write very much code.

Known. Java and OBJC support reflection. Of course, there are other languages supported, my knowledge level is limited. Please forgive me for not listing them completely.


Assuming that you want to implement reflection, you need to implement several functions such as the following:

Get an object pointer. Even if it is void. It is also possible to get its real class type name through some APIs. For example, Java can get its actual type of package path + class name

Get this object pointer and get all of its member variables. The name of the member function, the type (field), the number of parameters and the return type (function), returned as a string or other package type. The core is also a string.

Get the pointer to this object and be able to pass the string. Run a function. The ability to get the value of a member through a string.


Both Java and OBJC can support reflection, and another premise is that they all unify the base class. Java, for example, is called Java.lang.Object. OBJC inside is nsobject. With a unified base class, you can pre-define a set of reflection mechanisms and APIs. Whatever inherits the implementation class of this base class, it comes with an already implemented reflection function.

C + + does not have a standard base class, various frameworks, various platforms have a variety of frameworks, class libraries.

If we were to define a unified base class in a system that we actually developed, wouldn't it be in C + +. Realize the reflection function, even the awkward implementation is also good ah, this also can reduce very much bitter force C + + Developer's workload, ah, how good.

Give it a try.


First we are if the application contains all the classes. All have a common base class.

The second C + + member variables, member functions, objects, all of them, are pointer positioning.

So let's fix this by " an object pointer + a function pointer calling the function's problem ."

The full demo program is already written.

#include "stdafx.h" class Cxobject{public:int  dosum (int a, int b);}; typedef int (cxobject::* pf_dosum) (int, int); int Cxobject::d osum (int a, int b) {return a + B;} void Dosome (cxobject* pObj, pf_dosum func) {int result = (Pobj->*func) (+);p rintf ("result=%d", result);} int _tmain (int argc, _tchar* argv[]) {cxobject obj;d osome (&obj, &cxobject::d osum); GetChar (); return 0;}


Can see that the function can be called by an object pointer, plus a function pointer.

This is a C + + is also a non-common use of skills.

MFC inside the system generated a lot of code is in this way.


Our requirement is to call the function through a string, so we need to establish a mapping of the function name to the function pointer. The function pointer is then able to find the string, and the function pointer calls the function.

Please see the full code:

Test01.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <vector> #include <string>class cxobject; typedef void (cxobject::* Void_func _void) (void); Class cxobject{public://Gets the name of all functions of the class virtual void Getmethodnames (std::vector<std::string>& names);//According to the name, Get the function pointer virtual void_func_void getfuncbyname (std::string& name);}; void Cxobject::getmethodnames (std::vector<std::string>& names) {}void_func_void Cxobject::getfuncbyname ( std::string& name) {return NULL;} Class Cxmyimpl:public Cxobject{public://function required to implement//get the name of all functions of class virtual void Getmethodnames (std::vector<std::string >& names)//based on the name, get the function pointer virtual void_func_void getfuncbyname (std::string& name);p ublic://custom business functions. void DoPrint1 (); void DoPrint2 ();}; void Cxmyimpl::getmethodnames (std::vector<std::string>& names) {names.push_back ("doPrint1"); names.push_ Back ("DoPrint2");} Void_func_void cxmyimpl::getfuncbyname (std::string& name) {void_func_void Pfun = NULL; if (Name.compare ("DoPrint1" ) = = 0) Pfun = static_cast<void_func_void> (&cxmyimpl::d oPrint1); if (Name.compare ("doPrint2") = = 0) Pfun = static _cast<void_func_void> (&cxmyimpl::d oPrint2); return pfun;} void Cxmyimpl::d oPrint1 () {printf ("Cxmyimpl::d oPrint1 be called!! \ r \ n ");} void Cxmyimpl::d oPrint2 () {printf ("Cxmyimpl::d oPrint2 be called!! \ r \ n ");} int _tmain (int argc, _tchar* argv[]) {cxobject* obj = new Cxmyimpl ();//In the code behind it, there is no cxmyimpl,cxobject defined in DoPrint1, doprint2.//runs the DoPrint1 function through the doPrint1 of the string. std::vector<std::string> names; obj->getmethodnames (names); for (int idx=0; idx<names.size (); idx++) {void _func_void Pfun = Obj->getfuncbyname (Names[idx]);(obj->*pfun) (); GetChar (); return 0;}

The results of the execution are as follows:


As can be seen from the example above, it is actually possible to simulate the effect of reflection.


Assuming that C + + is fully implemented to facilitate a useful reflection mechanism, the following conditions are required:

1, the unified base class

2, the compiler to help


For the second condition, one can understand:

In the example above

void Cxmyimpl::getmethodnames (std::vector<std::string>& names) {names.push_back ("doPrint1"); names.push_ Back ("DoPrint2");} Void_func_void cxmyimpl::getfuncbyname (std::string& name) {void_func_void Pfun = NULL; if (Name.compare ("DoPrint1" ) = = 0) Pfun = static_cast<void_func_void> (&cxmyimpl::d oPrint1); if (Name.compare ("doPrint2") = = 0) Pfun = Static_cast<void_func_void> (&cxmyimpl::d oPrint2); return pfun;}

These two functions are defined within the base class, and are considered to be reflection-related APIs, but suppose I have to implement both functions myself for each class I write. Wouldn't it be exhausting.

But in turn, the functions of these two functions are very easy, the code is very regular. That is to record the current class has those functions, the name and function pointers corresponding. This process completely enables the compiler to add to me at the time of compiling.

There is one aspect, because to facilitate the demonstration. The business functions used here are all types of void f (void). There is no parameter and return value, which is obviously unreasonable. But it's not impossible, it's just that it's more complicated. Circle around a lot of other.


In the absence of such a compiler, in the absence of such a uniform base class library, the reflection of C + + is just stuck in the interest research phase, and there is no real value at the moment. Personal feeling of Ha.

Programming Fundamentals--c++ can I support the reflection of Java and OBJC?

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.