Re: Dynamic creation of objects by class name

From: Mike Gleason Jr Couturier (mikejrMAPSON_at_videotron.ca)
Date: 12/09/04


Date: Thu, 9 Dec 2004 15:39:00 -0500

Yup !

template<class T> CType * f(){CType *p = new T();return p;};

int main(int argc, char* argv[])
{
    CType *t = f<CDerived>();
}

(I really talked to myself ! please ignore post !)

"Mike Gleason Jr Couturier" <mikejrMAPSON@videotron.ca> wrote in message
news:eWchu4i3EHA.2572@TK2MSFTNGP11.phx.gbl...
> I just though that I can use templates...
>
> "Mike Gleason Jr Couturier" <mikejrMAPSON@videotron.ca> wrote in message
> news:Ozu6P1i3EHA.3236@TK2MSFTNGP15.phx.gbl...
> > Let me give an example to clarify !!
> >
> > MyBaseClass *d1 = CreateDerived(CMyDerivedClass1);
> >
> > where the parameter to the function would really be the
> > derived class name...
> >
> > "Mike Gleason Jr Couturier" <mikejrMAPSON@videotron.ca> wrote in message
> > news:eDb%232wi3EHA.2572@tk2msftngp13.phx.gbl...
> > > Is it possible to dynamically create objects in plain
> > > c++ and rtti like in MFC when we pass the RUNTIME_CLASS()
> > > of a class to AfxBeginThread (ui thread) ?
> > >
> > > I would like a function to do this :
> > >
> > > MyBaseClass * CreateDerived(CMyDerivedClass1);
> > > MyBaseClass * CreateDerived(CMyDerivedClass2);
> > >
> > > where both CMyDerivedClass1 and 2 are deriving
> > > from MyBaseClass
> > >
> > > In the function, I would like to do something like this :
> > > MyBaseClass *p = new <the_derived_class_parameter>
> > > return p;
> > >
> > > Is it possible to achieve this ?
> > > Thanks
> > >
> > > Mike
> > >
> > >
> >
> >
>
>



Relevant Pages