![]() |
calling cunstroctor from CString object
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........ |
Re: calling cunstroctor from CString object
On Jan 11, 11:11 am, "NightCrawler" <muley.ra...@gmail.com> wrote:
> Hi > can i call a constructor of a class, if the name is stored in a CString Object What you want is called reflection, and it's not available in C++. However if you sit down and think you'll probably be able to come up with a solution that does not require such a thing by redesigning your application. -- Erik Wikström |
Re: calling cunstroctor from CString object
> 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....."; Any way you need to do this if, else checking to determine the class name. Instead of setting strClassName variable, you can set an enumeration/int variable have a switch case Check out here http://home.earthlink.net/~huston2/d...MethodDemosCpp Not sure how much this helps but this is how I would have done. Thanks, Satish NightCrawler wrote: > 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........ |
Re: calling cunstroctor from CString object
Satish wrote:
> > //now i want to allocate the memory for the class whose name is stored > > pPtr = new "some way to call the constructor whose name is stored in > > Check out here > http://home.earthlink.net/~huston2/d...MethodDemosCpp Yes, factory can help you, if number of classes is not very big. Factory in the case will use links between "signs" of class and metods of class creation, for example: template<class Trequested> class creator { const char* name; //any uniq string int number; //any uniq number Trequested* create(){ new Trequested; } }; |
| All times are GMT. The time now is 05:59 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.