RE: [Dev-C++] Help with creating objects dynamically
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Somanathan K. <som...@wi...> - 2003-07-22 05:41:42
|
Hi Gurus,
I would like to take this discussion to another turn,
Say if I have requirement like creating an object dynamically, means not
the object name but the class definition needs to be dynamically chosen,
for eg:- let me talk in C++ language=20
Class worker : public base
{
}
class clerk : public base
{
}
class manager : public base
{
}
// create an array of string
string emp[3] =3D {"worker","clerk","manager"};=20
some_func(int i)
{
base b* =3D new emp[i];
b->any_common_func();
}
--------------
This avoids lots of case statements, makes it easy to maintain and
extend.=20
Any body has any idea how to implement it in C++(equivalent to
Class.forname in JAVA)
If you have any other idea to accomplish this, also welcome.
Let me know if things are not clear.
Thanks
Somanathan
-----Original Message-----
From: Marc Heiligers [mailto:ma...@e-...]=20
Sent: Tuesday, July 22, 2003 9:49 AM
To: 'Satish Medepalli'; dev...@li...
Subject: RE: [Dev-C++] Help with creating objects dynamically
Hi Sateesh,
I think what you're looking for is the STL std::map which serves as a
kind
of associative array. There are other guys on this list more educated in
the
ways of STL than I. I'm sure they will help you out if you have
problems.=20
__________________________________
Marc Heiligers
-----Original Message-----
Hello Marc,
Thank you for your reply. There is no real necessity for me to do so..
I
can make it work using a hash table or some kind of a mapping function..
I
just thought that if it was possible in the way I earlier asked, I could
save some execution time as there will not be much need for searching
operation..
As i got almost every feedback saying that this is not possible, I
think I
will stick to a hash table solution.
Thank you for all your help with this.
-Sateesh Medepalli
--- Marc Heiligers <ma...@e-...> wrote:
> I can see absolutely no requirement to do this at all. In addition=20
> (and to answer the question) I can see absolutely no way of doing it=20
> either. Scripting languages that have some kind of eval statement=20
> could do this, but
> again, I can see no requirement for it (although I have used a similar
> mechanism in a JScript ASP project to create objects of a particular
> name,
> but never dependant on data within the object itself).=20
>=20
> Maybe if you enlighten us on why you would want to do this, we could=20
> help you find a solution.
>=20
> __________________________________
> Marc Heiligers
>=20
> -----Original Message-----
>=20
> I have the following requirement while creating objects.. I will=20
> try to explain it using a small example..
>=20
> Suppose I have a class Student defined and I want to create objects
> of type Student inside the main function..
>=20
> And say, the actual name with which i want to create the obect is=20
> stored in a character array at run time.. say i have the following=20
> statement char NAME[]=3DgetName();
> and the above function is returning the name "JOHN" at run time.
>=20
> Now i want to get the name out of the character array and create=20
> the object using that particular name at run time.. i.e., Student=20
> JOHN;
>=20
> in a nutshell i want to give the name of the objects to be created=20
> at run time. IS THIS POSSIBLE?? IF SO CAN ANYONE HELP ME WITH THIS??
>=20
> Thank You in advance.
> -Sateesh Medepalli
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|