如何用它的名称实例化一个类?提前阅读我的文字...... [英] How can I instantiate a class with it's name? read my text ahead...

查看:65
本文介绍了如何用它的名称实例化一个类?提前阅读我的文字......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:D


我有一个包含类名的字符串。

我想从该类中实例化:< br $>

string strategyNameStr;

策略*策略;


//以下所有类都来自策略。

//和" strategyNameStr"""包含以下类之一的

之一的名称。

//例如

strategyNameStr = _T(" SimpleStrategy");


SimpleStrategy * simStrategy;

MiddleStrategy * midStrategy;

HighStrategy * highStrategy;


如何我可以使用" strategyNameStr"""在下一行......

策略=新的?




有没有解决方案?


谢谢。

解决方案

ba ******** @ yahoo.com 写道:


我有一个包含名称的字符串一个班级。

我想从该班级实例化:


string strategyNameStr;

策略*策略;


//以下所有类都来自策略。

//和" strategyNameStr"""包含以下类之一的

之一的名称。

//例如

strategyNameStr = _T(" SimpleStrategy");


SimpleStrategy * simStrategy;

MiddleStrategy * midStrategy;

HighStrategy * highStrategy;


如何我可以使用" strategyNameStr"""在下一行......

策略=新的?




有什么解决方案吗?



#define INSTANTIATE_BY_NAME(var,str,clazz)\

if(str == #clazz)var = new clazz


策略= 0;

INSTANTIATE_BY_NAME(策略,strategyNameStr,SimpleStrategy);

if(!strategy)

INSTANTIATE_BY_NAME(策略,strategyNameStr,MiddleStrategy);

if(!策略)

INSTANTIATE_BY_NAME(策略,策略名称,高级策略);


或类似的东西。


V

-

请在回复时删除资金''A'通过电子邮件

我没有回复最热门的回复,请不要问


2007-12-12 15 :48:47-505,Victor Bazarov < v。******** @ comAcast.netsaid:


>

#define INSTANTIATE_BY_NAME(var,str ,clazz)\

if(str == #clazz)var = new clazz



你不需要Java-拼写错误的类型。

预处理器不关心关键字。


-

Pete

Roundhouse咨询有限公司( www.versatilecoding.com
的作者
标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


Pete Becker写道:


在2007-12-12 15:48:47 -0500,Victor Bazarov

< v。******** @ comAcast.netsaid:
< blockquote class =post_quotes>
>>
#define INSTANTIATE_BY_NAME(var,str,clazz)\
if(str == #clazz)var = new clazz



你不需要Java拼写错误的拼写错误。

预处理器并不关心关键字。



我的编辑器。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Hi everybody:D

I''ve a string that contains the name of a class.
I want to instantiate from that class:

string strategyNameStr;
Strategy * strategy;

// All of the following classes are derived from Strategy.
// and """ strategyNameStr """ contains the name of one of the
following classes.
// e.g.
strategyNameStr = _T( "SimpleStrategy" );

SimpleStrategy * simStrategy;
MiddleStrategy * midStrategy;
HighStrategy * highStrategy;

How can I use """ strategyNameStr """ in the next line...
strategy = new ?

or
Is there at all any solution?

Thanks.

解决方案

ba********@yahoo.com wrote:

I''ve a string that contains the name of a class.
I want to instantiate from that class:

string strategyNameStr;
Strategy * strategy;

// All of the following classes are derived from Strategy.
// and """ strategyNameStr """ contains the name of one of the
following classes.
// e.g.
strategyNameStr = _T( "SimpleStrategy" );

SimpleStrategy * simStrategy;
MiddleStrategy * midStrategy;
HighStrategy * highStrategy;

How can I use """ strategyNameStr """ in the next line...
strategy = new ?

or
Is there at all any solution?

#define INSTANTIATE_BY_NAME(var, str, clazz) \
if (str == #clazz) var = new clazz

strategy = 0;
INSTANTIATE_BY_NAME(strategy, strategyNameStr, SimpleStrategy);
if (!strategy)
INSTANTIATE_BY_NAME(strategy, strategyNameStr, MiddleStrategy);
if (!strategy)
INSTANTIATE_BY_NAME(strategy, strategyNameStr, HighStrategy);

or something like that.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On 2007-12-12 15:48:47 -0500, "Victor Bazarov" <v.********@comAcast.netsaid:

>
#define INSTANTIATE_BY_NAME(var, str, clazz) \
if (str == #clazz) var = new clazz

You don''t need that Java-esque ugliness of misspelling class. The
preprocessor doesn''t care about keywords.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


Pete Becker wrote:

On 2007-12-12 15:48:47 -0500, "Victor Bazarov"
<v.********@comAcast.netsaid:

>>
#define INSTANTIATE_BY_NAME(var, str, clazz) \
if (str == #clazz) var = new clazz


You don''t need that Java-esque ugliness of misspelling class. The
preprocessor doesn''t care about keywords.

My editor does.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于如何用它的名称实例化一个类?提前阅读我的文字......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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