Thread: 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-21 11:07:18
|
This is a strange requirement. Name of the object is normally irrelevant = compared to its definition. Do you mind in sharing your requirement? -----Original Message----- From: OROSZI Bal=E1zs [mailto:or...@vp...]=20 Sent: Monday, July 21, 2003 4:26 PM To: dev...@li... Subject: Re: [Dev-C++] Help with creating objects dynamically Satish Medepalli wrote: > I have the following requirement while creating objects.. I will = 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 > stored in a character array at run time.. say i have the following > 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 = the > object using that particular name at run time.. i.e.,=20 > Student JOHN; =20 >=20 > in a nutshell i want to give the name of the objects to be created = at > run time. IS THIS POSSIBLE?? IF SO CAN ANYONE HELP ME WITH THIS?? Nope. As far as I know, this is not possible in C++. I know, that Perl=20 can do it, but if you EVER programmed in Perl, you know how different it = is. So I don't think it ca nbe done. A solution to your problem might be to give a "name" member to your=20 class, then at object creation, the constructor takes it as parameter,=20 and copies it into the name member. -- Greetings, Bal=E1zs ------------------------------------------------------- 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 |
|
From: xRxIxPx <xr...@gm...> - 2003-07-21 16:39:42
|
At 16:36 2003-07-21 +0530, Somanathan Kaimal wrote:
>This is a strange requirement. Name of the object is normally irrelevant=20
>compared to its definition. Do you mind in sharing your requirement?
>
>
>-----Original Message-----
>From: OROSZI Bal=E1zs [mailto:or...@vp...]
>Sent: Monday, July 21, 2003 4:26 PM
>To: dev...@li...
>Subject: Re: [Dev-C++] Help with creating objects dynamically
>
>Satish Medepalli wrote:
> > I have the following requirement while creating objects.. I will try=
to
> > explain it using a small example..
> >
> > Suppose I have a class Student defined and I want to create objects=
of
> > type Student inside the main function..
> >
> > And say, the actual name with which i want to create the obect is
> > stored in a character array at run time.. say i have the following
> > statement
> > char NAME[]=3DgetName();
> > and the above function is returning the name "JOHN" at run time.
> >
> > Now i want to get the name out of the character array and create the
> > object using that particular name at run time.. i.e.,
> > Student JOHN;
> >
> > in a nutshell i want to give the name of the objects to be created at
> > run time. IS THIS POSSIBLE?? IF SO CAN ANYONE HELP ME WITH THIS??
>
>Nope. As far as I know, this is not possible in C++. I know, that Perl
>can do it, but if you EVER programmed in Perl, you know how different it
>is. So I don't think it ca nbe done.
>
>A solution to your problem might be to give a "name" member to your
>class, then at object creation, the constructor takes it as parameter,
>and copies it into the name member.
Should work.
Well better: use a map. Something like this should work, though not tested=
=20
right now.
STL is a wonderful tool.;)
#include <map>
#include <string>
#include "MyObjectClass.h"
//and then have you object database
std::map<std::string, MyObjectClass*> obj_db;
void add_to_db()
{
MyObjectClass* obj =3D new MyObjectClass;
obj_db.insert(std::map<std::string, MyObjectClass*>::value_type(=20
std::string(getName()), obj));
}
MyObjectClass& get_from_db(const std::string& name)
{
std::map<std::string, MyObjectClass*>::iterator it;
it =3D obj_db.find(name);
if (it =3D=3D map.end()) throw std::string("name not found int=
obj_db");
return *it;
}
|
|
From: xRxIxPx <xr...@gm...> - 2003-07-22 08:30:50
|
At 18:40 2003-07-21 +0200, xRxIxPx wrote:
>At 16:36 2003-07-21 +0530, Somanathan Kaimal wrote:
>
>#include <map>
>#include <string>
>
>#include "MyObjectClass.h"
>//and then have you object database
>
>std::map<std::string, MyObjectClass*> obj_db;
>
>void add_to_db()
>{
> MyObjectClass* obj = new MyObjectClass;
> obj_db.insert(std::map<std::string, MyObjectClass*>::value_type(
> std::string(getName()), obj));
>}
>
>MyObjectClass& get_from_db(const std::string& name)
>{
> std::map<std::string, MyObjectClass*>::iterator it;
> it = obj_db.find(name);
> if (it == map.end()) throw std::string("name not found int obj_db");
>
> return *it;
>}
D`oh. s/return *it/return **it/
And deletion:
void delete_from_db(const std::string& name)
{
std::map<std::string, MyObjectClass*>::iterator it;
it = obj_db.find(name);
if (it == map.end()) throw std::string("name not found int obj_db");
delete *it;
obj_db.erase(it);
}
|
|
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
|
|
From: Marc H. <ma...@e-...> - 2003-07-22 08:12:42
|
Hi Somanathan,
There's probably a million different ways of doing this. I would =
probably
start by creating a NewPerson method defined something like (and =
defines):
#define PERSON_WORKER 1
#define PERSON_CLERK 2
#define PERSON_MANAGER 3
base* NewPerson(int nType)
{
switch(nType)
{
case PERSON_WORKER:
return (base*) new worker;
case PERSON_CLERK:
return (base*) new clerk;
case PERSON_MANAGER:
return (base*) new manager;
default:
return (base*) NULL;
}
}
And then your function becomes:
some_func(int i)
{
base *b =3D NewPerson(i);
b->any_common_func();
}
Then you only have to maintain the switch in NewPerson().
__________________________________
Marc Heiligers
-----Original Message-----
From: Somanathan Kaimal [mailto:som...@wi...]=20
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
|
|
From: Jaap de W. <wo...@ne...> - 2003-07-22 07:10:57
|
Below is a piece of code what is creating named objects dynamicaly.
Is that what you mean?
> -----Original Message-----
> From: Satish Medepalli [mailto:sat...@ya...]
> Sent: Monday, July 21, 2003 6:28 AM
> To: dev...@li...
> Subject: [Dev-C++] Help with creating objects dynamically
>
>
> Hello,
>
> I have the following requirement while creating objects..
> I will try to
> explain it using a small example..
>
> Suppose I have a class Student defined and I want to
> create objects of
> type Student inside the main function..
>
> And say, the actual name with which i want to create the obect is
> stored in a character array at run time.. say i have the following
> statement
> char NAME[]=getName();
> and the above function is returning the name "JOHN" at run time.
>
> Now i want to get the name out of the character array and
> create the
> object using that particular name at run time.. i.e.,
> Student JOHN;
>
> in a nutshell i want to give the name of the objects to be
> created at
> run time. IS THIS POSSIBLE?? IF SO CAN ANYONE HELP ME WITH THIS??
>
> Thank You in advance.
> -Sateesh Medepalli
>
=================================== start of code
===================================
#include <list>
#include <string>
#include <iostream>
class Object
{
public:
std::string &GetName(){return m_name;}
virtual std::string &GetFunction()=0;
protected:
Object(const char *name):m_name(name){}
Object(const Object &src):m_name(src.m_name){}
private:
std::string m_name;
friend std::ostream &operator <<(std::ostream &os, Object& ol);
};
class Student: public Object {
public:
virtual std::string &GetFunction(){return m_function;}
Student(const char *name):Object(name) {}
Student(const Student& src):Object(src) {}
private:
static std::string m_function;
};
class Teacher: public Object {
public:
virtual std::string &GetFunction(){return m_function;}
Teacher(const char *name):Object(name) {}
Teacher(const Teacher& src):Object(src) {}
private:
static std::string m_function;
};
std::string Student::m_function ("Student");
std::string Teacher::m_function ("Teacher");
class ObjectList: public std::list<Object *>
{
public:
~ObjectList();
};
ObjectList::~ObjectList()
{
iterator i;
for (i = begin(); i != end(); i++)
{
delete *i;
}
}
std::ostream &operator <<(std::ostream &os, Object& ob)
{
return os << ob.m_name << " (" << ob.GetFunction() << ")";
}
std::ostream &operator <<(std::ostream &os, ObjectList& ol)
{
ObjectList::iterator i;
for (i = ol.begin(); i != ol.end(); i++)
{
os << **i << std::endl;
}
return os;
}
void main()
{
ObjectList AllPeople;
AllPeople.push_back(new Student("Jan"));
AllPeople.push_back(new Student("Piet"));
AllPeople.push_back(new Student("Klaas"));
AllPeople.push_back(new Teacher("Jan"));
AllPeople.push_back(new Teacher("Mark"));
std::cout << AllPeople;
}
|
|
From: Somanathan K. <som...@wi...> - 2003-07-22 08:36:43
|
Hi Marc,
Thanks for the suggestion. The switch statement is the one I want to
avoid in real time. As when the type of message (worker,clerk etc here)
more and more switch statements needs to be added. If you know the
working of Class.forname, you will get to know what I mean. One way I
can achieve this is by pooling the object(this is how normally people
does for real time saving) but would like to know there is an equivalent
to Class.forname. I will rewrite your code if C++ has Class.forname
#define PERSON_WORKER 1
#define PERSON_CLERK 2
#define PERSON_MANAGER 3
base* NewPerson(int nType)
{
return(base*) Class.forname(person_types[i];// person_types has array
of =20
//strings, say "worker, clerk etc. Please note
that //as and when the number
of types
//increased this line of code won't be
//changed but only the array index needs
//to be created
}
And then your function becomes:
some_func(int i)
{
base *b =3D NewPerson(i);
b->any_common_func();
}
Does it makes any sense?
somanathan
-----Original Message-----
From: Marc Heiligers [mailto:ma...@e-...]=20
Sent: Tuesday, July 22, 2003 1:42 PM
To: Somanathan Kaimal; dev...@li...
Subject: RE: [Dev-C++] Help with creating objects dynamically
Hi Somanathan,
There's probably a million different ways of doing this. I would
probably
start by creating a NewPerson method defined something like (and
defines):
#define PERSON_WORKER 1
#define PERSON_CLERK 2
#define PERSON_MANAGER 3
base* NewPerson(int nType)
{
switch(nType)
{
case PERSON_WORKER:
return (base*) new worker;
case PERSON_CLERK:
return (base*) new clerk;
case PERSON_MANAGER:
return (base*) new manager;
default:
return (base*) NULL;
}
}
And then your function becomes:
some_func(int i)
{
base *b =3D NewPerson(i);
b->any_common_func();
}
Then you only have to maintain the switch in NewPerson().
__________________________________
Marc Heiligers
-----Original Message-----
From: Somanathan Kaimal [mailto:som...@wi...]=20
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
|
|
From: Per W. <pw...@ia...> - 2003-07-22 11:37:46
|
No, there are no Class.forname.
But if you register all your classes into a map, you could create one
yourself:
#include <map>
#include <string>
class Base {
public:
virtual void Something() {}
};
typedef Base* (*PeopleFactory)();
class PeopleTypes {
protected:
std::map<std::string,PeopleFactory> factories;
public:
void Register(const char *type,Base* (*factory)());
Base* Allocate(const char *type);
};
extern PeopleTypes people_types;
class Student : public Base {
protected:
static Base* Factory() { return new Student; }
class RegisterStudent {
public:
RegisterStudent() {
people_types.Register("Student",Student::Factory);
}
}
static RegisterStudent register;
public:
...
};
Student::RegisterStudent Student::register; // Register "Student" factory
PeopleTypes people_types; // Keep map of factories
int main() {
Base *p = people_types.Allocate("Student");
p->Somethong();
delete p;
return 0;
}
/Per W
On Tue, 22 Jul 2003, Somanathan Kaimal wrote:
> Hi Marc,
>
> Thanks for the suggestion. The switch statement is the one I want to
> avoid in real time. As when the type of message (worker,clerk etc here)
> more and more switch statements needs to be added. If you know the
> working of Class.forname, you will get to know what I mean. One way I
> can achieve this is by pooling the object(this is how normally people
> does for real time saving) but would like to know there is an equivalent
> to Class.forname. I will rewrite your code if C++ has Class.forname
>
> #define PERSON_WORKER 1
> #define PERSON_CLERK 2
> #define PERSON_MANAGER 3
> base* NewPerson(int nType)
> {
> return(base*) Class.forname(person_types[i];// person_types has array
> of
> //strings, say "worker, clerk etc. Please note
> that //as and when the number
> of types
> //increased this line of code won't be
> //changed but only the array index needs
> //to be created
> }
>
> And then your function becomes:
>
> some_func(int i)
> {
> base *b = NewPerson(i);
> b->any_common_func();
> }
>
> Does it makes any sense?
>
> somanathan
>
> -----Original Message-----
> From: Marc Heiligers [mailto:ma...@e-...]
> Sent: Tuesday, July 22, 2003 1:42 PM
> To: Somanathan Kaimal; dev...@li...
> Subject: RE: [Dev-C++] Help with creating objects dynamically
>
> Hi Somanathan,
>
> There's probably a million different ways of doing this. I would
> probably
> start by creating a NewPerson method defined something like (and
> defines):
>
> #define PERSON_WORKER 1
> #define PERSON_CLERK 2
> #define PERSON_MANAGER 3
> base* NewPerson(int nType)
> {
> switch(nType)
> {
> case PERSON_WORKER:
> return (base*) new worker;
> case PERSON_CLERK:
> return (base*) new clerk;
> case PERSON_MANAGER:
> return (base*) new manager;
> default:
> return (base*) NULL;
> }
> }
>
> And then your function becomes:
>
> some_func(int i)
> {
> base *b = NewPerson(i);
> b->any_common_func();
> }
>
> Then you only have to maintain the switch in NewPerson().
> __________________________________
> Marc Heiligers
>
>
> -----Original Message-----
> From: Somanathan Kaimal [mailto:som...@wi...]
>
>
>
> 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
>
> Class worker : public base
> {
> }
> class clerk : public base
> {
> }
> class manager : public base
> {
> }
> // create an array of string
> string emp[3] = {"worker","clerk","manager"};
>
> some_func(int i)
> {
> base b* = new emp[i];
> b->any_common_func();
> }
>
> --------------
> This avoids lots of case statements, makes it easy to maintain and
> extend.
> 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
>
>
>
>
> -------------------------------------------------------
> 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
>
|