By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

How do I create a class from a string at runtime ?

P: n/a
Hello,

I've seen some vague references on how to do this a factory, but not
in enough detail to create one, or even know if it's what I need.

Essentially, I'll have one of about 5 classes that could be referenced
as a string (from a file).

I'm not 100% sure of the relationship between the classes, but I
suspect that they will not be derived from a base class, but in this
case there is not a reason not to either, if it makes a difference.

Any help in pointing me in the direction of taking a string and
instantiating an object at runtime would be greatly helpful.

Thanks

Matt

Oct 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
TazaTek <ma**@tazatek.comwrote:
I've seen some vague references on how to do this a factory, but not
in enough detail to create one, or even know if it's what I need.

Essentially, I'll have one of about 5 classes that could be referenced
as a string (from a file).

I'm not 100% sure of the relationship between the classes, but I
suspect that they will not be derived from a base class, but in this
case there is not a reason not to either, if it makes a difference.

Any help in pointing me in the direction of taking a string and
instantiating an object at runtime would be greatly helpful.
Base* newObject( const string& s )
{
Base* result = 0;
if ( s == "classA" )
result = new ClassA;
else if ( s == "classB" )
result = new ClassB;
return result;
}
Oct 9 '07 #2

P: n/a
Base* newObject( const string& s )
{
Base* result = 0;
if ( s == "classA" )
result = new ClassA;
else if ( s == "classB" )
result = new ClassB;
return result;

}
That makes sense.

Is there a way to implement this dynamically?

for example, if I later determined that I could have 100 classes
(instead of the 5 I mention previously) that could be used (not likely
as I'd re-design) and a case statement would become quite burdensome
to maintain.

I thought somewhere there was a Factory pattern (or some other
pattern) that could be used for this, but I can't seem to connect the
dots and get the dynamic situation that I want. I know this is
(fairly) common, but I'm drawing a blank.

Thanks

Matt


Oct 9 '07 #3

P: n/a
TazaTek <ma**@tazatek.comwrote:
Base* newObject( const string& s )
{
Base* result = 0;
if ( s == "classA" )
result = new ClassA;
else if ( s == "classB" )
result = new ClassB;
return result;

}

That makes sense.

Is there a way to implement this dynamically?
I think the answer to that depends on the development environment rather
than the language.

for example, if I later determined that I could have 100 classes
(instead of the 5 I mention previously) that could be used (not likely
as I'd re-design) and a case statement would become quite burdensome
to maintain.
There are ways of avoiding the case statement. For example creating a
map where the key is the string and the value is an object of the
appropriate type (or a builder of an object of the appropriate type.)
Then you could do something like this:

class Builder {
public:
virtual Base* create() = 0;
};

class Factory {
map<string, Builder*builders;
public:
void addBuilder( string s, Builder* b ) {
builders[s] = b;
}
Base* build( string s ) {
base* result = 0;
map<string, Builder*>::iterator it = builders.find( s );
if ( it != builders.end() )
result = it->second->create();
return result;
}
};

With just a little bit of creativity, each of your 100 classes can have
a global object associated with it that automatically inserts itself
into the Factory as a builder of that class. In this way classes can be
added and removed from the system at link time without having to change
the Factory code. This doesn't really count as dynamic because it can
only be done during program construction though...

Frankly though, I don't see the point. The code that knows what string
to pass to the Factory could just as easily new the object it needs
directly since it obviously knows what the type of the object is.
(Unless of course, the string was input from outside the system.)
Oct 9 '07 #4

P: n/a
TazaTek wrote:
>Base* newObject( const string& s )
{
Base* result = 0;
if ( s == "classA" )
result = new ClassA;
else if ( s == "classB" )
result = new ClassB;
return result;

}

That makes sense.

Is there a way to implement this dynamically?

for example, if I later determined that I could have 100 classes
(instead of the 5 I mention previously) that could be used (not likely
as I'd re-design) and a case statement would become quite burdensome
to maintain.

I thought somewhere there was a Factory pattern (or some other
pattern) that could be used for this, but I can't seem to connect the
dots and get the dynamic situation that I want. I know this is
(fairly) common, but I'm drawing a blank.
Look a bit deeper, the factory pattern is what you are after. One
simple solution is a map of function pointers or objects that create
instances of the required classes.

--
Ian Collins.
Oct 9 '07 #5

P: n/a
Frankly though, I don't see the point. The code that knows what string
to pass to the Factory could just as easily new the object it needs
directly since it obviously knows what the type of the object is.
(Unless of course, the string was input from outside the system.)
That is precisely my problem. The string is in the form of a URL, and
must be mapped to a class and method to invoke.

The example you give gives me some ideas on how to proceed, and filled
in a precious piece that I didn't find elsewhere. Thanks.

Looks like I just need to dig deeper into the Factory explainations.

Matt

Oct 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.