动态类加载 [英] Dynamic Class Loading
问题描述
我之前的例子使用了Shape类层次结构的概念,所以我将继续使用它。
假设我有五十种不同形状的东西,我正在尝试将b $ b实例化其中一个。诀窍是,instatiation将基于一个
字符串,其名称与我试图实例化的类型完全相同。
显然,写了50个元素长switch语句是一个劣等的
解决方案。那么,我如何基于字符串实例化?标准中是否提供了一种方法
,或者我是没有划桨的小溪?
Aguilar, James写道:我之前的例子使用了Shape类层次结构的概念,所以我将继续这样做。
假设我有五十种不同的形状,我正在尝试实例化其中一个。诀窍是,instatiation将基于一个
字符串,其名称与我尝试实例化的类型完全相同。
类型的名称仅存在于源代码中。一旦代码已经编译并链接了
,就没有类型名称,并且通常还有
也没有可区分的类型。只是一堆函数和const
数据控制程序的行为,直到运行时数据
进来。
显然,写一个五十元素的长开关语句是一个劣等的解决方案。
这是AFA C ++的唯一解决方案。关于主题的变化
包括表格,地图等,但基本上它们导致相同的任务:
字符串与某些函数的关联导致实例化
相应的一个或多个类。
那么,我如何基于字符串进行实例化?是否有标准中提供的方法,或者我是没有划桨的小溪吗?
语言中没有提供关联字符串的方法任何
类型,除非你自己做。不要打电话给简单的编程任务
在没有桨的情况下上一条小溪。免费奶酪只存在于捕鼠器中。
V
" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
news:jI ***************** @ ord-read.news.verio.net ...Aguilar,James写道:
[snip]
好的,所以没有语言结构可以让我这样做。我想,第二个最好的想法是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)将解析为指向该方法的
,除非我的语法错误。请更正
我,如果我没有正确的话。
Aguilar,James写道:
我之前的例子使用了Shape类层次结构的概念,所以我将继续这样做。
假设我有五十种不同的形状,我正在努力
实例化其中一个。诀窍是,instatiation将基于
一个字符串,其名称与我尝试
实例化的类型完全相同。显然,编写一个50元长的switch语句是一个劣等的解决方案。那么,我如何基于字符串实例化?是否有标准中提供的方法,或者我是没有划桨的小溪吗?
读取/设计模式/,并使用原型,类工厂或工厂
方法。
另外,为什么需要实例?每个班级真的有州吗?可以
你使用Flyweight吗?
-
Phlip
http://industrialxp.org/community/bi...UserInterfaces
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?
Aguilar, James wrote: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.
The name of a type only exists in the source code. Once the code has
been compiled and linked, there are no type names, and often there are
no distinguishable types either. Just a bunch of functions and const
data that control the behaviour of the program until the run-time data
come in.
Obviously, writing a fifty element long switch statement is an inferior
solution.
It''s the only solution, AFA C++ is concerned. Variations on the theme
include tables, maps, etc., but essentially they lead to the same task:
association of a string with some function that causes the instantiation
of the corresponding class or classes.
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
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:jI*****************@ord-read.news.verio.net...Aguilar, James wrote:
[snip]
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.
Aguilar, James wrote:
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?
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
这篇关于动态类加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!