Type, GetType(), and more.....

L

Lord2702

Fri. Aug. 20, 2004 10:50 AM PT

I want to create an Object/ Class from its name as string. I will explain
it....

Lets say I have a class BaseClass, and from this class I derived 5 classes
Derived_1, Derived_2,.....,Derived_5. All these classes are already defined
in a library.

Now, I want to create Derived_2 class just by its name string "Derived_2".
One possible work arround this problem is, to add a suffix to each derived
class, like MyDerived_1, MyDerived_2,..... and then use the TokenPasting to
create a class.

#define CLASS_NAME(__CLS__) My##__CLS__

and use it like this

BaseClass *derived = new CLASS_NAME("Derived_2")();

This works fine, but here you have to passed actual string literal, and I
want to pass a string variable, lets say, strDerivedClassName. This way
token-Pasting will not work. I have seen that, when you create a __gc
object, it has a method, ToString(), which returns class name as string.

public __gc class Person { };
Person *p = new Person();
p->ToString(); //prints Person as string.

Is there any way to create a class instance, which is already defined
somewhere, with its name as a string ?

Thanks in Advance.
 
H

Hendrik Schober

Lord2702 said:
Fri. Aug. 20, 2004 10:50 AM PT

I want to create an Object/ Class from its name as string. [...]

Is that C++ or .NET?
C++ lacks the sophisticated reflection to
do this with what's built into the language,
but there are ways to do this.
For example, you could setup a map of strings
to functions that create the repsective
objects.
Use google to find the Factory Pattern.

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
L

Lord2702

Dear Hendrick,
Thanks, for your reply.

In C++ you have RTTI, which also returns typeid info, but, my this question
is for .Net 2003

Yes with map it is possible, but in that case I have to create my Derived
objects and then stored into the map object, this sounds like if I have 20
Derived object, all I have to create and store them in Map object, with
their name.

Hendrik Schober said:
Lord2702 said:
Fri. Aug. 20, 2004 10:50 AM PT

I want to create an Object/ Class from its name as string. [...]

Is that C++ or .NET?
C++ lacks the sophisticated reflection to
do this with what's built into the language,
but there are ways to do this.
For example, you could setup a map of strings
to functions that create the repsective
objects.
Use google to find the Factory Pattern.

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
T

Tomas Restrepo \(MVP\)

public __gc class Person { };
Person *p = new Person();
p->ToString(); //prints Person as string.

If you are using just .NET and __gc classes, you can use
System.Activator.CreateInstance() for this... That's what it is for..
 
H

Hendrik Schober

Lord2702 said:
Dear Hendrick,
Thanks, for your reply.

In C++ you have RTTI, which also returns typeid info, but, my this question

Yes, but that cannot be used to create
objects unless you put some code around
it for yourself.
but, my this question
is for .Net 2003

I have no idea of this.
Yes with map it is possible, but in that case I have to create my Derived
objects and then stored into the map object, this sounds like if I have 20
Derived object, all I have to create and store them in Map object, with
their name.

You wouldn't have to put those objects
into the map, but functors which create
them. This could be plain C function
pointers or function objects.


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top