动态实例化一个类 [英] dynamically instantiate a class

查看:68
本文介绍了动态实例化一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的应用程序在运行时从文件中读取类名称(作为字符串)。

我可以创建一个这个类的实例?


谢谢

Sumit

Hi All,

My application reads a ''Class'' name (as a string) from a file at Run time.
Can i create an instance this Class ?

Thanks
Sumit

推荐答案



" Sumit Nagpal" <苏*** @ noida.atrenta.com>在消息中写道

新闻:2t ************* @ uni-berlin.de ...

"Sumit Nagpal" <su***@noida.atrenta.com> wrote in message
news:2t*************@uni-berlin.de...
大家好,
我的应用程序在运行时从文件中读取类名称(作为字符串)。
我可以创建此类的实例吗?
Hi All,

My application reads a ''Class'' name (as a string) from a file at Run time.
Can i create an instance this Class ?



当然。


std :: string classname;

std :: ifstream instream(" myfile");

instream>> classname;

myClass * mc = 0;


if(classname ==" myClass")

mc = new myClass ;


-Mike



Sure.

std::string classname;
std::ifstream instream("myfile");
instream >> classname;
myClass *mc = 0;

if(classname == "myClass")
mc = new myClass;

-Mike


Sumit Nagpal写道:
Sumit Nagpal wrote:
我的应用程序读取运行时文件中的类名称(作为字符串)。
我可以创建此类的实例吗?
My application reads a ''Class'' name (as a string) from a file at Run time.
Can i create an instance this Class ?




查找工厂模式和Google.com的原型模式


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces




" Sumit Nagpal"写道:

"Sumit Nagpal" wrote:
我的应用程序在运行时从文件中读取一个''Class''名称(作为一个字符串)。
我可以创建一个这个类的实例吗?
My application reads a ''Class'' name (as a string) from a file at Run time.
Can i create an instance this Class ?



你必须添加基础设施:创建对象工厂

获取字符串并输出堆分配的类。


/ Pavel


You must add infrastructure: create object factory which
takes string and outputs heap allocated class.

/Pavel


这篇关于动态实例化一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆