Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!uflorida!beach.cis.ufl.edu!rmy From: rmy@beach.cis.ufl.edu (Rasthiyaadu Yakaa) Newsgroups: comp.lang.c++ Subject: Generically (Interactively) invoking Constructors ?? Message-ID: <23861@uflorida.cis.ufl.EDU> Date: 16 Jul 90 20:00:27 GMT Sender: news@uflorida.cis.ufl.EDU Reply-To: rmy@beach.cis.ufl.edu () Distribution: na Organization: UF CIS Department Lines: 65 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