在运行时构造类 [英] Construction of classes at runtime

查看:82
本文介绍了在运行时构造类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




无论如何构建一个类的对象,其类名为

动态获得?可以使用Prototype和Factory方法以及Abstract

工厂设计模式来完成它。但它涉及在工厂级别使用

if-else语句。为了避免使用if-else

语句(在我的情况下会有很多),是否有任何方法可以根据字符串变量的值构造对象
?因此,当类名分配给字符串变量时,

,字符串

值(类名)可用于构造对象。


您的回复表示赞赏。


谢谢,

萨米

Hi,

Is there anyway to construct a object of class with its class name
obtained dynamically? Prototype and Factory method along with Abstract
factory design pattern can be used to do it. But it involves using of
if-else statement at the factory level. To avoid using the if-else
statements (there would be plenty in my case), is there any way to
construct the object based on the value of a string variable ? So that
when the class name is assigned to the string variable, the string
value (class name) can be used in construction of the object.

Your response is appreciated.

Thanks,
Sami

推荐答案

8月20日,2:41 * pm,Sami< s.saminat ... @ gmail.comwrote:
On Aug 20, 2:41*pm, Sami <s.saminat...@gmail.comwrote:




无论如何构造一个类的对象,其类名为

动态获得?可以使用Prototype和Factory方法以及Abstract

工厂设计模式来完成它。但它涉及在工厂级别使用

if-else语句。为了避免使用if-else

语句(在我的情况下会有很多),是否有任何方法可以根据字符串变量的值构造对象
?因此,当类名分配给字符串变量时,

,字符串

值(类名)可用于构造对象。


您的回复表示赞赏。


谢谢,

Sami
Hi,

Is there anyway to construct a object of class with its class name
obtained dynamically? Prototype and Factory method along with Abstract
factory design pattern can be used to do it. But it involves using of
if-else statement at the factory level. To avoid using the if-else
statements (there would be plenty in my case), is there any way to
construct the object based on the value of a string variable ? So that
when the class name is assigned to the string variable, the string
value (class name) can be used in construction of the object.

Your response is appreciated.

Thanks,
Sami



所需的实现是用C ++编写的,因此发布在这个组中。

谢谢。

The implementation needed is in C++, hence posted in this group.
Thanks.


Sami写道:
Sami wrote:




无论如何构造一个类的对象,其类名为

动态获得?可以使用Prototype和Factory方法以及Abstract

工厂设计模式来完成它。但它涉及在工厂级别使用

if-else语句。为了避免使用if-else

语句(在我的情况下会有很多),是否有任何方法可以根据字符串变量的值构造对象

Hi,

Is there anyway to construct a object of class with its class name
obtained dynamically? Prototype and Factory method along with Abstract
factory design pattern can be used to do it. But it involves using of
if-else statement at the factory level. To avoid using the if-else
statements (there would be plenty in my case), is there any way to
construct the object based on the value of a string variable ?



使用字符串(名称)和工厂对象的地图来创建所需的

类。


你不能用它的名字在C ++中创建一个''on''',你必须在编译时知道这些类。


-

Ian Collins。

Use a map of strings (names) and factory objects to create the required
class.

You can''t create a class ''on the fly'' from its name in C++, you have to
know the classes at compile time.

--
Ian Collins.


8月20日上午10:41 * am,Sami< s.saminat .. 。@ gmail.comwrote:
On Aug 20, 10:41*am, Sami <s.saminat...@gmail.comwrote:




无论如何构造一个类的对象及其类名

动态获得?可以使用Prototype和Factory方法以及Abstract

工厂设计模式来完成它。但它涉及在工厂级别使用

if-else语句。为了避免使用if-else

语句(在我的情况下会有很多),是否有任何方法可以根据字符串变量的值构造对象
?因此,当类名分配给字符串变量时,

,字符串

值(类名)可用于构造对象。


您的回复表示赞赏。


谢谢,

Sami
Hi,

Is there anyway to construct a object of class with its class name
obtained dynamically? Prototype and Factory method along with Abstract
factory design pattern can be used to do it. But it involves using of
if-else statement at the factory level. To avoid using the if-else
statements (there would be plenty in my case), is there any way to
construct the object based on the value of a string variable ? So that
when the class name is assigned to the string variable, the string
value (class name) can be used in construction of the object.

Your response is appreciated.

Thanks,
Sami



虚拟拷贝构造函数或克隆函数可能对您有所帮助吗?

A virtual copy constructor, or a clone function might help you?


这篇关于在运行时构造类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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