日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++反射機(jī)制具體實(shí)現(xiàn)方法詳解

C++編程語言是一款功能強(qiáng)大的計(jì)算機(jī)應(yīng)用語言。其能夠支持很多程序設(shè)計(jì)風(fēng)格。我們今天將會在這里為大家詳細(xì)介紹一下有關(guān)C++反射機(jī)制的具體實(shí)現(xiàn)步驟,大家可以從中獲得一些有幫助的內(nèi)容。

創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元都江堰做網(wǎng)站,已為上家服務(wù),為都江堰各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

在Java編程中,我們經(jīng)常要用到反射,通過反射機(jī)制實(shí)現(xiàn)在配置文件中的靈活配置, 但在C++編程中,對這種方式步提供現(xiàn)有的支持,那么我們怎么才能在配置文件中配置想要調(diào)用的對象呢?

我們的思路是通過對象名稱確定對象實(shí)例,把對象名和對象實(shí)例通過哈希表進(jìn)行映射,那么我們就可以實(shí)現(xiàn)通過對象名稱獲取對象了。首先定義一個(gè)C++反射機(jī)制的結(jié)構(gòu):

 
 
 
  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. };

其中Register這樣定義

 
 
 
  1. bool Register(ClassInfo* ci);

然后定義一個(gè)類,頭文件如下:

 
 
 
  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 m_classInfoMap;
  11. };

cpp文件如下:

 
 
 
  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. m_classInfoMap[classInfo->Type] = classInfo; 
  10. return true; 
  11. }
  12. DynBase* DynBase::CreateObject(string type)
  13. {
  14. if ( m_classInfoMap[type] != NULL ) 
  15. return m_classInfoMap[type]->Fun();
  16. }
  17. return NULL;
  18. }

那么我們在C++反射機(jī)制的操作中實(shí)現(xiàn)映射的類只要繼承于DynBase就可以了,比如由一個(gè)類CIndustryOperate

頭文件如下

 
 
 
  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. };

cpp文件如下:

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

以上就是我們?yōu)榇蠹以敿?xì)介紹的C++反射機(jī)制的實(shí)現(xiàn)方法。


網(wǎng)站題目:C++反射機(jī)制具體實(shí)現(xiàn)方法詳解
地址分享:http://www.5511xx.com/article/coigose.html