Re: calling constructor from CString
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 13 Jan 2007 22:26:08 -0500
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
.
- References:
- calling constructor from CString
- From: NightCrawler
- calling constructor from CString
- Prev by Date: Re: Batch file and MFC
- Next by Date: Re: investigate threads
- Previous by thread: Re: calling constructor from CString
- Next by thread: What does "System Resource Exceeded" mean?
- Index(es):
Relevant Pages
|