473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
5 1908
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
7
by: Dave Taylor | last post by:
I have a DataTable with three string columns, "Value", "Format" and "Type" However, some of the rows contain numbers and dates in the Value field. So I would like to be able to format the output...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
7
by: MarkoH | last post by:
Wsdl.exe /server creates abstract class derived from WebService. Is there a way to create this class at runtime based on some WSDL file given at runtime ? What would be even better - creating...
0
by: davezhang | last post by:
I am trying to create a COM object library that is language independent. So that other developer can use it in orther developing environment like VB 6.0 or C++, it can be built successfully, however,...
6
by: py_genetic | last post by:
Is this possible or is there a better way. I need to create a new class during runtime to be used inside a function. The class definition and body are dependant on unknows vars at time of exec,...
1
by: henrymania | last post by:
Am writing a code for database backup....by backupservlet is as given below i get the following exception
1
by: jxh00u | last post by:
Hi guys, I am new to C# and a have a question around class instances. I have a class, Pizza. To create an instance I would use: Pizza pizza1 = new Pizza("Cheese"); Lets suppose I wanted...
7
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>;...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.