This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: create an object with variable CLASS


Hi Mohsen
On Wed, Oct 28, 2009 at 12:46:56AM +0330, Mohsen Pahlevanzadeh wrote:
> Dear Axel, 
> given code doesn't work.
I tried it now - it compiles and runs fine for me with gcc-4.4.1.
What errors do you obtain?

Axel
> On Mon, 2009-10-26 at 17:32 +0100, Axel Freyn wrote:
> > Hi Mohsen,
> > On Tue, Oct 27, 2009 at 07:40:45AM +0330, Mohsen Pahlevanzadeh wrote:
> > > Suppose following code:
> > > ////////////
> > > char *var;
> > > var = "class1"
> > > ////////////
> > > So, I don't know name of class1, but i have varibale var.How can i
> > > create an object from class1 by this variavble?
> > Sorry, now I understand the question:-)
> > Well - I think the best way is the one proposed by Andrew: You first
> > have to some kind of dictionary, which maps the names to the classes -
> > this dictionary can't be created automatically in C++, but you have to
> > write it yourself. And in addition, you have to supply a common
> > base-class for all your classes. The following example (untested...) 
> > should explain the procedure:
> > 
> > #include <iostream>
> > #include <cstring>
> > #include <cstdlib>
> > class Base{
> >   public:
> >     virtual void DoSomething() = 0;
> > };
> > class A:public Base{
> >   public:
> >     void DoSomething(){std::cout << "A" << std::endl;}
> > };
> > class B:public Base{
> >   public:
> >     void DoSomething(){std::cout << "B" << std::endl;}
> > };
> > Base *Create(const char *name){
> >   if (strcmp(name, "A")==0)
> >     return new A;
> >   if (strcmp(name, "B")==0)
> >     return new B;
> >   exit(1);
> > }
> > int main(){
> >   Base *base;
> >   base = Create("A");
> >   base->DoSomething();
> >   delete base;
> >   base = Create("B");
> >   base->DoSomething();
> >   delete base;
> > }
> > 
> > Axel
> > 
> > > 
> > > 
> > > On Mon, 2009-10-26 at 10:55 +0100, Axel Freyn wrote:
> > > > Hi,
> > > > On Tue, Oct 27, 2009 at 12:31:05AM +0330, Mohsen Pahlevanzadeh wrote:
> > > > > Oh, I forgot that i say i need to variable name of objects.same classes.
> > > > > Therefor i need to variable name of objects & variable name of classes.
> > > > > On Mon, 2009-10-26 at 23:42 +0330, Mohsen Pahlevanzadeh wrote:
> > > > > > Dear all,
> > > > > > We know that we can create an object with following code:
> > > > > > /////////////////////////////////////////////////
> > > > > > class_name *object_name = new() class_name;
> > > > > > ///////////////////////////////////////////////
> > > > > > So, i have a class pool,I wanna create Object with a variable name that
> > > > > > variable is my class.Same:
> > > > > > var *obj = new() var;
> > > > > > var is a variable that contains a my class's name.
> > > > > > How can i implement it?
> > > > Well, I'm not sure I understand correctly, but I think the solution is
> > > > derivation:
> > > > class Base{};
> > > > class A:public Base{};
> > > > class B:public Base{};
> > > > ...
> > > > 
> > > > 
> > > > Then you can do:
> > > > Base *var;
> > > > var = new A;
> > > > delete var;
> > > > var = new B;
> > > > delete B;
> > > > 
> > > > All functions, which you want to call by accessing "var" (e.g.
> > > > var->Function()) have to be defined as virtual function in class Base.
> > > > 
> > > > Does that solve your problem?
> > > > 
> > > > Axel
> > > 
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]