Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   calling cunstroctor from CString object (http://www.velocityreviews.com/forums/t459775-calling-cunstroctor-from-cstring-object.html)

NightCrawler 01-11-2007 10:11 AM

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........


=?iso-8859-1?q?Erik_Wikstr=F6m?= 01-11-2007 10:36 AM

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


Satish 01-11-2007 02:38 PM

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........



Grizlyk 01-14-2007 08:24 AM

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:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.