是否可以通过名称创建类的实例? (C ++) [英] is it possible to create an instance of a class by its name? (c++)

查看:66
本文介绍了是否可以通过名称创建类的实例? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:),
我有Java方面的经验,现在正在使用c ++进行项目.
该程序由一些命令组成,每个命令都是一个类
(或实际上是一个实例).我的问题是要为进一步的维护和扩展而设计出出色的设计(例如,应添加20条新命令,并删除其中一些命令,越容易完成越好).

一段时间以前,我在另一个用Java编写的应用程序中遇到了这个问题.我用类/命令的名称创建了一个新实例,它看起来像这样:

Hi :),
I have experience in Java and now working with a project in c++.
The program consist of a few commands there every command is a class
(or actually an instance of one). My problem is to get a great design for further maintenance and expansion (e.g. say 20 new commands should be added and some removed, the easier the better this is done).

For a time ago I faced this problem in an other application written in Java. Were I created a new instance by the name of the class/command, it looked something like this:

<br />
Command c = (Command)Class.forName(str).newInstance();<br />
c.runCommand(str_args);<br />



是否有可能在c ++中做出类似的事情,或者我必须选择
其他解决方案? :sigh:

如果没有,那么您如何在c ++中有效地实现这一点,所以您
不需要非常长的列表(如果需要的话)或不需要的解决方案
每次添加命令都会进行很多更改和扩展吗?

(会有很多命令(> 100))
(也许我应该提一下,它不是Visual c ++ .net,而是win的本机c ++)

预先感谢,如果您可以带上一些东西,我将非常高兴.
很好的答案. //WaZoX



Would it be possible to make something similar in c++ or must I choose
an other solution? :sigh:

If not, then how do you implement this effective in c++ so you
don''t need an extremely long (if else) list or a solution that needs
a lot of changes and extensions every time a command is added?

(there will be a lot commands (>100))
(maybe I should mention it''s not visual c++ .net but native c++ for win)

Thanks in advance, I would be very happy if you could come with some
great answers. //WaZoX

推荐答案

简短回答,否:C++不支持该功能.

我认为您应该看看原型设计模式 [
Short answer, no: C++ has no support for that.

I think you should have a look at prototype design pattern[^] (or, more generally to creational design patterns).
:)


除非您将命令保存用于其他有害目的(即,它们已经执行了撤消操作,并且一旦执行了它们,它们就会卡在撤消操作上)堆栈,以防用户想要退出),您真正想要的是某种基于字符串执行任意代码块的方法.

一种方法是使用映射或其他关联数组:

Unless you''re saving your commands for some other nefarious purpose (i.e. they''ve got an undo operation and once they''re carried out they get stuck on an undo stack in case the user wants to back out) what you''re really after is some way of executing an arbitrary chunk of code based on a string.

One way of doing this is to use a map, or other associative array:

typedef std::map< std::string, command * > command_map;
command_map cmd_map;



其中的命令类似于:



where command is something like:

class command
{
    public:
        virtual ~command() {}
        virtual void execute() = 0;
};



然后使用它来查找并执行要执行的命令:



and then use that to look up and execute what command you want executed:

void execute_command_by_name( const std::string &cmd_name,
                              const command_map *cmd_map )
{
    command_map::const_iterator iter( cmd_map.find() );
    if( iter != cmd_map.end() && iter->second )
    {
        iter->second->execute();
    }
}



如果您确实需要某种创建模式来生成命令,则使用创建命令的函数或函子替换映射中指向该命令的指针,您将获得几乎相同的效果.如果您想走得更远,甚至可以在运行时动态加载命令的代码,但是该操作非常特定于OS.

顺便说一句,我不确定在这种情况下是否可以使用PROTOTYPE模式-当您可以使用原始对象时,您确实不需要类的示例来克隆.如果您需要创建命令,如上所述,我将亲自使用FACTORY METHOD而不是PROTOTYPE.

干杯,



PS:编辑时,我实际上是在谈论工厂方法"而不是抽象工厂".屁股.



If you really need some sort of creational pattern to generate commands then replace the pointer to the command in the map with a function or functor that creates commands and you''ll get much the same effect. If you want to still go further you can even dynamically load the code for commands at runtime but how to do that is very OS specific.

Anwyay, I''m not sure that PROTOTYPE pattern is the one to use in this case - you don''t really need an exemplar for your class to clone when you can use the original object. If you need to create commands, as I mentioned above, I''d personally use FACTORY METHOD rather than PROTOTYPE.

Cheers,

Ash

PS: Edit as I was actually talking about FACTORY METHOD not ABSTRACT FACTORY. Bum.


另请参见DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE
的示例 在<afx.h>中(如果有的话):)
See also the example of DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE
in the <afx.h> if you have it :)


这篇关于是否可以通过名称创建类的实例? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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