Re: calling constructor from CString

Tech-Archive recommends: Fix windows errors by optimizing your registry



DECLARE_DYNCREATE in your classes, which you will derive from CObject. Then you can
create them not by name but by RUNTIME_CLASS name.

The CRuntimeClass::CreateObject method will, given a RUNTIME_CLASS value, create an object
of the appropriate type. This is all explained in the MSDN; start with CObject, go to
dynamic object creation, and follow links.
joe

"NightCrawler" <muley.rahul@xxxxxxxxx> wrote in message
news:1168510192.425716.113890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi
can i call a constructor of a class, if the name is stored in a CString
Object
e.g.

class CBaseClass
{
int x;
};

class CDerClass1: public CBaseClass
{
int y;
};

class CDerClass2: public CBaseClass
{
int z;
};

CString strClassName;
// = L"CMyClass";//using unicode charset....

//main1//i know this


CBaseClass* pPtr;
------
----
----------

if(some condition)
{
strClassName = L"CDerClass1"
pPtr = new CDerClass1;
}
else
{
strClassName = L"CDerClass2"
pPtr = new CDerClass2;
}
//for 2 classes this approach is fine but
//what if i have more than 100 classes derived from the same class
//n at runtime i want to allocate the memory for them.....

//main2//can it be done????


CBaseClass* pPtr;
------
----
----------

if(some condition)
{
strClassName = L"CDerClass1"
}
else
{
strClassName = L"CDerClass2"
}
//now i want to allocate the memory for the class whose name is stored
in the strClassName
pPtr = new "some way to call the constructor whose name is stored in
string.....";


plz help me i m tired of writting this if else statements........


Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages