Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I instantiate a class with it's name? read my text ahead...

Reply
Thread Tools

How can I instantiate a class with it's name? read my text ahead...

 
 
babakandme@yahoo.com
Guest
Posts: n/a
 
      12-12-2007
Hi everybody

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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-12-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      12-12-2007
On 2007-12-12 15:48:47 -0500, "Victor Bazarov" <(E-Mail Removed)> said:

>
> #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)

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-12-2007
Pete Becker wrote:
> On 2007-12-12 15:48:47 -0500, "Victor Bazarov"
> <(E-Mail Removed)> said:
>>
>> #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


 
Reply With Quote
 
babakandme@yahoo.com
Guest
Posts: n/a
 
      12-12-2007
Hi,
thaks for the helps, but consider here:
The solution is somehow """HardCoding"""...


// Manager.h
typedef map< string, SStrategy * > SStrategyPool;
....
SStrategyPool SSPool;

// Manager.cpp
Manager::SomeFunction()
{
SStrategyPool::const_iterator iter;

// FlyWeighy pattern
iter = SSPool.find( strategyNameStr );

// if the strategy has not been added to the pool
if( iter == SSPool.end() )
{

/
*================================================= =============================
Actually the problem is here, as you see, it is difficult to use some
"HardCoded" code here.
I mean the number of strategys may increase too much...
and it's not nice to use "HardCoding..."...
================================================== ============================*/
// sStrategy = new ...?

// Add the new Strategy to the pool
SSPool.insert( strategyNameStr, sStrategy );

// call the desired SStrategy->DoIt();
sStrategy->DoIt();

}
else
{
// Use the old Added Strategy to the pool
// call the desired SStrategy->DoIt();
( iter->second )->DoIt();
}
}
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      12-12-2007
On 2007-12-12 16:42:05 -0500, "Victor Bazarov" <(E-Mail Removed)> said:

> Pete Becker wrote:
>> On 2007-12-12 15:48:47 -0500, "Victor Bazarov"
>> <(E-Mail Removed)> said:
>>>
>>> #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.
>


Sigh. Yet another "expert" on how people should write code. <g>

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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-12-2007
Pete Becker wrote:
> On 2007-12-12 16:42:05 -0500, "Victor Bazarov"
> <(E-Mail Removed)> said:
>> Pete Becker wrote:
>>> On 2007-12-12 15:48:47 -0500, "Victor Bazarov"
>>> <(E-Mail Removed)> said:
>>>>
>>>> #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.
>>

>
> Sigh. Yet another "expert" on how people should write code. <g>


LOL

You and your duplicities! Did you mean that I'm the "expert" or
that the editor is?

I don't mean I have an editor who tells me how to write (I guess
you had one when you published your "Extensions", perhaps he/she
was too eager to edit, eh?). I actually meant the text editor
with syntax highlighting that makes "class" to show in bold type,
which I don't care for in that context.

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


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      12-12-2007
On 2007-12-12 17:57:49 -0500, "Victor Bazarov" <(E-Mail Removed)> said:

> Pete Becker wrote:
>> On 2007-12-12 16:42:05 -0500, "Victor Bazarov"
>> <(E-Mail Removed)> said:
>>> Pete Becker wrote:
>>>> On 2007-12-12 15:48:47 -0500, "Victor Bazarov"
>>>> <(E-Mail Removed)> said:
>>>>>
>>>>> #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.
>>>

>>
>> Sigh. Yet another "expert" on how people should write code. <g>

>
> LOL
>
> You and your duplicities! Did you mean that I'm the "expert" or
> that the editor is?


Whoops, sorry: I meant the editor.

>
> I don't mean I have an editor who tells me how to write (I guess
> you had one when you published your "Extensions", perhaps he/she
> was too eager to edit, eh?).


No, actually, the copy editors on my book were terrific. I learned a
lot, and the book is much better for it.

> I actually meant the text editor
> with syntax highlighting that makes "class" to show in bold type,
> which I don't care for in that context.
>


Yes, I knew that.

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

 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      12-13-2007
dont you prefer a factory?

class strategyBuilder{
public:
static strategy* function buildStrategy(string objName)
{
if (objname == "sim")
return static_cast<strategy*>(new SimpleStrategy());
else if (objname == "mid")
return static_cast<strategy*>(new MiddleStrategy());

return NULL;
}
or something like that.

you could use it in the form of class/global function and you would
have to worry about pesky macro bugs.
 
Reply With Quote
 
babakandme@yahoo.com
Guest
Posts: n/a
 
      12-13-2007
Yeah my Man:>
thanks...

Actually first I didn't want to use any HardCoding...
But now I see, here the FactoryMethod Pattern works...

Once again, thank you in advance...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
can't instantiate following inner class Pyenos Python 5 12-28-2006 11:46 AM
Re: Can't instantiate class David Mitchell Python 1 11-06-2005 07:21 PM
Can't instantiate class David Mitchell Python 0 11-06-2005 02:25 PM
Instantiate class within a class Colin Mc Mahon ASP General 2 08-13-2004 11:17 PM



Advertisments