问题描述
我之前的例子使用了Shape类层次结构的概念,所以我将继续使用它。
假设我有五十种不同形状的东西,我正在尝试将b $ b实例化其中一个。诀窍是,instatiation将基于一个
字符串,其名称与我试图实例化的类型完全相同。
显然,写了50个元素长switch语句是一个劣等的
解决方案。那么,我如何基于字符串实例化?标准中是否提供了一种方法
,或者我是没有划桨的小溪?
语言中没有提供关联字符串的方法任何
类型,除非你自己做。不要打电话给简单的编程任务
在没有桨的情况下上一条小溪。免费奶酪只存在于捕鼠器中。
V
好的,所以没有语言结构可以让我这样做。我想,第二个最好的想法是HashMap,它包含字符串和类。但是,正如你所说的那样,没有办法在C ++中简单地表示一个类型。那么我应该把什么作为数据放在hashmap中?
?指向构造函数的指针?我不认为
有效,是吗?那么,指向静态成员方法的指针呢?东西
喜欢这个:
//在Something.h
class东西:public Foo
{
public:
void method1();
Something(int n);
static Something * initThis(int n);
}
//在Something.cpp中
static Something * Something :: initThis(int n)
{
返回新的东西(n);
}
// In程序运行时填充hashmap的方法
void fillHashMap()
{
...
map.put(" Something",(Something :: initThis));
...
//这假定map是< string的类型的模板,东西*(*)(int n)>
}
//然后,获取实例化Something的方法:
void someothermethod(string classToGet)
{
Something *(initFun *)(int n)= map.get(classToGet);
}
那段代码可行,对吗? (Something :: initThis)(int n)将解析为指向该方法的
,除非我的语法错误。请更正
我,如果我没有正确的话。
读取/设计模式/,并使用原型,类工厂或工厂
方法。
另外,为什么需要实例?每个班级真的有州吗?可以
你使用Flyweight吗?
-
Phlip
My previous example used the concept of a Shape class heirarchy, so I will
continue with that.
Suppose I have something like fifty different shapes, and I am trying to
instantiate one of them. The trick is, the instatiation will be based on a
string with the exact same name as the type that I am trying to instantiate.
Obviously, writing a fifty element long switch statement is an inferior
solution. So, how can I instantiate based on strings? Is there a method
provided in the standard, or am I up a creek without a paddle?
No method is provided in the language to associate strings with any
types unless you do it yourself. Do not call simple programming task
"up a creek without a paddle". Free cheese exists only in a mousetrap.
V
OK, so there''s no language construct that lets me do that. The second best
idea is a HashMap, I guess, with the string and the class. However, as you
said, there is no way to represent simply a type in C++. So what should I
put as the data in the hashmap? A pointer to a constructor? I don''t think
that works, does it? So, a pointer to a static member method? Something
like this:
//In Something.h
class Something : public Foo
{
public:
void method1();
Something(int n);
static Something* initThis(int n);
}
//In Something.cpp
static Something* Something::initThis(int n)
{
return new Something(n);
}
//In the method that fills the hashmap when the program runs
void fillHashMap()
{
...
map.put("Something", (Something::initThis));
...
//This assumes map is a template of type <string, Something* (*)(int n)>
}
//Then, to get the method that would instantiate Something:
void someothermethod(string classToGet)
{
Something* (initFun*)(int n) = map.get(classToGet);
}
That code would work, right? (Something::initThis)(int n) would resolve to
a pointer to that method, unless I''ve got my syntax wrong. Please correct
me if I don''t have it right.
Read /Design Patterns/, and use either Prototype, Class Factory, or Factory
Method.
Also, why do you need an instance? Does each class really have state? Could
you use Flyweight?
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
这篇关于动态类加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!