rmy@beach.cis.ufl.edu (Rasthiyaadu Yakaa) (07/17/90)
Hi,
I am having a problem, trying to generically call constructors
in an application program. My problem is that i am not sure if
C++ allows what i want to do, and it it does, i'd like to know
how. So, here goes ..
I want to write an application program that can generically invoke
constructors (and thus do initialization) for a group of classes.
More specifically, the program will request (interactively) from
the user the name of a class, and a list of values for the instance
variables of the class. The program should then create a new instance
of the specified class, and initialize the instance variables to the
specified value. e.g. there are 1000 classes representing different
kinds of animals (which may or may not be derived from the same base
class). The user provides a class name (as a string) say, Kangaroo.
The program is able to look up ( from a table ) the instance variables
and their types, and interactively queries the user to enter values for
these instance variables. Now, the program has the name of the class,
as a string, and set of (void) pointers appropriately casted to contain
the values of the instance variables.
Is it now possible to invoke the 'new' operator using this information ??
here's what i want to do (it probably looks silly, but conveys the idea)
char * class_name;
class_name = "Kangaroo"; // string initialized by input from user
void x;
x = (int) 9; // say class Kangaroo has only one instance variable
// to be set to 9
void y;
y = (*class_name) (new (*class_name) ((int)x));
^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
^ ^
| |
try to cast try to invoke new operator
to class Kangaroo for class Kangaroo
while syntactically this looks FUNNY and STUPID, the question is
is there a way to generate the class identifier from a string
(e.g. generate int from "int", or float from "float") and
secondly, to use them, if it is possible to generate them (e.g.
to use them in a casting as above).
An answer by e-mail would be greatly appreciated. Reply to
rmy@beach.cis.ufl.edu or rmy@trout.cis.ufl.edu.
BTW, i have started programming in C++ only recently, and also, my
experience in C is not too much. So pardon me, if the question has
been posed in an absurd-sounding manner.
regards
yakaa