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.
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.