calling cunstroctor from CString object

Discussion in 'C++' started by NightCrawler, Jan 11, 2007.

  1. NightCrawler

    NightCrawler Guest

    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........
    NightCrawler, Jan 11, 2007
    #1
    1. Advertising

  2. On Jan 11, 11:11 am, "NightCrawler" <> 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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 11, 2007
    #2
    1. Advertising

  3. NightCrawler

    Satish Guest

    > 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/dp/FactoryMethodDemosCpp

    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........
    Satish, Jan 11, 2007
    #3
  4. NightCrawler

    Grizlyk Guest

    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/dp/FactoryMethodDemosCpp


    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; }
    };
    Grizlyk, Jan 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    4,524
  2. yeye yaya

    I hate cstring!!!

    yeye yaya, Jul 11, 2003, in forum: C++
    Replies:
    7
    Views:
    2,696
    Stuart Golodetz
    Jul 11, 2003
  3. Rajesh Garg

    Why is CString not preferred

    Rajesh Garg, Jul 22, 2003, in forum: C++
    Replies:
    11
    Views:
    793
    Mike Wahler
    Jul 24, 2003
  4. Markus Hämmerli
    Replies:
    6
    Views:
    7,769
    Peter van Merkerk
    Sep 4, 2003
  5. John Smith

    Parsing char array to CString

    John Smith, Sep 6, 2003, in forum: C++
    Replies:
    4
    Views:
    7,229
    Pat McCormack
    Sep 9, 2003
Loading...

Share This Page