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