惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
P
Privacy International News Feed
Security Latest
Security Latest
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Last Watchdog
The Last Watchdog
S
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Cyberwarzone
Cyberwarzone
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
A
Arctic Wolf
博客园 - 聂微东
I
Intezer
腾讯CDC
罗磊的独立博客
T
Tailwind CSS Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
N
News and Events Feed by Topic
SecWiki News
SecWiki News
S
Security Affairs
T
Threat Research - Cisco Blogs
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
T
Troy Hunt's Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
I
InfoQ
Hacker News: Ask HN
Hacker News: Ask HN
T
The Blog of Author Tim Ferriss
Schneier on Security
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - 吴碧宇

NPM私有服务器架设 FOR CentOS 【转】c# 位操作 C#中调用Windows API时的数据类型对应关系 全面理解javascript的caller,callee,call,apply概念[转载] 发几个小的测式软件 用户中心 - 博客园 Vs2010 配置驱动开发环境 美杜杉 主动防御最新版 Asp.net 异步请求 IHttpAsyncHandler [转]COM线程模型-套间 [转]VC使用CRT调试功能来检测内存泄漏 [转]c#中 uint--byte[]--char[]--string相互转换汇总 [转]C++深度探索系列:智能指针(Smart Pointer) [一] [转]DirectShow+VS2005配置日记 [转]Windows API 注册表函数 特殊符号表 ATL 开发 Com 学习笔记 常用数据类型使用转换详解 - 吴碧宇 - 博客园 [转]BSTR、char*和CString short转换
【转】C++反射机制的实现
吴碧宇 · 2009-07-04 · via 博客园 - 吴碧宇

 1 在Java编程中,我们经常要用到反射,通过反射机制实现在配置文件中的灵活配置, 但在C++编程中,对这种方式步提供现有的支持,那么我们怎么才能在配置文件中配置想要调用的对象呢?
 2 
 3 我们的思路是通过对象名称确定对象实例,把对象名和对象实例通过哈希表进行映射,那么我们就可以实现通过对象名称获取对象了。首先定义一个结构:
 4 
 5 struct ClassInfo
 6 
 7 {
 8 public:
 9  
10  string Type;
11  funCreateObject Fun;
12  ClassInfo(string type, funCreateObject fun)
13  {
14   Type = type;
15   Fun = fun;
16   Register(this);
17  }
18 };
19 
20 其中Register这样定义
21 
22 bool Register(ClassInfo* ci);
23 
24 然后定义一个类,头文件如下:
25 
26 class AFX_EXT_CLASS DynBase  
27 {
28 public:
29  DynBase();
30  virtual ~DynBase();
31 
32 public
33  static bool Register(ClassInfo* classInfo);
34  static DynBase* CreateObject(string type);
35 
36 private
37  static std::map<string,ClassInfo*> m_classInfoMap;
38 };
39 
40 cpp文件如下:
41 
42 std::map< string,ClassInfo*> DynBase::m_classInfoMap = std::map< string,ClassInfo*>();
43 
44 DynBase::DynBase()
45 {
46 
47 }
48 
49 DynBase::~DynBase()
50 {
51 
52 }
53 
54 bool DynBase::Register(ClassInfo* classInfo)
55 
56  m_classInfoMap[classInfo->Type] = classInfo; 
57  return true
58 }
59 
60 DynBase* DynBase::CreateObject(string type)
61 {
62  if ( m_classInfoMap[type] != NULL ) 
63  { 
64   return m_classInfoMap[type]->Fun();
65  }
66  return NULL;
67 }
68 
69 那么我们实现映射的类只要继承于DynBase就可以了,比如由一个类CIndustryOperate
70 
71 头文件如下
72 
73 class CIndustryOperate : public DynBase
74 {
75 public:
76  CIndustryOperate();
77  virtual ~CIndustryOperate();
78 
79  static DynBase* CreateObject(){return new CIndustryOperate();}
80 
81 private:
82  static ClassInfo* m_cInfo;
83 };
84 
85 cpp文件如下:
86 
87 ClassInfo* CIndustryOperate::m_cInfo = new ClassInfo("IndustryOperate",(funCreateObject)( CIndustryOperate::CreateObject));
88 
89 CIndustryOperate::CIndustryOperate()
90 {
91 
92 }
93 
94 CIndustryOperate::~CIndustryOperate()
95 {
96 
97 }

98 这样CIndustryOperate这个类就真正实现了反射机制!来源:http://blog.csdn.net/iwouldwin/archive/2006/08/24/1113002.aspx