Detailed description of the implementation of the C ++ reflection mechanism

Source: Internet
Author: User

C ++ programming language is a powerful computer application language. It supports many programming styles. We will introduce the specific implementation steps of the C ++ reflection mechanism here today. You can get some helpful content.

In Java programming, reflection is often used to implement flexible configuration in the configuration file through the reflection mechanism. However, in C ++ programming, existing support is provided for this method step, so how can we configure the objects to be called in the configuration file?

Our idea is to determine the object instance through the object name, and map the Object Name and object instance through the hash table, then we can get the object through the object name. First, define a structure of the C ++ reflection mechanism:

 
 
  1. struct ClassInfo  
  2. {  
  3. public:  
  4. string Type;  
  5. funCreateObject Fun;  
  6. ClassInfo(string type, funCreateObject fun)  
  7. {  
  8. Type = type;  
  9. Fun = fun;  
  10. Register(this);  
  11. }  
  12. }; 

Here, Register is defined as follows:

 
 
  1. bool Register(ClassInfo* ci); 

Define a class. The header file is as follows:

 
 
  1. class AFX_EXT_CLASS DynBase   
  2. {  
  3. public:  
  4. DynBase();  
  5. virtual ~DynBase();  
  6. public:   
  7. static bool Register(ClassInfo* classInfo);  
  8. static DynBase* CreateObject(string type);  
  9. private:   
  10. static std::map<string,ClassInfo*> m_classInfoMap;  
  11. }; 

The cpp file is as follows:

 
 
  1. std::map< string,ClassInfo*> DynBase::m_classInfoMap = 
    std::map< string,ClassInfo*>();  
  2. DynBase::DynBase()  
  3. {  
  4. }  
  5. DynBase::~DynBase()  
  6. {  
  7. }  
  8. bool DynBase::Register(ClassInfo* classInfo)  
  9. {   
  10. m_classInfoMap[classInfo->Type] = classInfo;   
  11. return true;   
  12. }  
  13. DynBase* DynBase::CreateObject(string type)  
  14. {  
  15. if ( m_classInfoMap[type] != NULL )   
  16. {   
  17. return m_classInfoMap[type]->Fun();  
  18. }  
  19. return NULL;  

The ing Class in the C ++ reflection mechanism can be inherited from DynBase, for example, CIndustryOperate.

The header file is as follows:

 
 
  1. class CIndustryOperate : public DynBase  
  2. {  
  3. public:  
  4. CIndustryOperate();  
  5. virtual ~CIndustryOperate();  
  6. static DynBase* CreateObject(){return new CIndustryOperate();}  
  7. private:  
  8. static ClassInfo* m_cInfo;  
  9. }; 

The cpp file is as follows:

 
 
  1. ClassInfo* CIndustryOperate::m_cInfo = new ClassInfo
    ("IndustryOperate",(funCreateObject)( CIndustryOperate::
    CreateObject));  
  2. CIndustryOperate::CIndustryOperate()  
  3. {  
  4. }  
  5. CIndustryOperate::~CIndustryOperate()  
  6. {  

The above is the implementation method of the C ++ reflection mechanism.

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.