Is there bettor solution or any simple template implementation?

I have one function with 3 parameters.
In that function I have one switch case statement which depends on first parameter of my function. Nearly the cases are 28 to 30. For each case there is another sub switch case which depends on the second parameter of my function with more cases. For different cases I am allocating different classes memory in one class object. At last the function returning that object. For this concept implementation is there any simple implementation or any other logic, any template. The cases are increasing because of new implementations. Expecting some better solution.

The sample code is:
[code]CMyCls* CEmr:GetCls(DWORD dwDId, DWORD dwClsId,BOOL bFe)
{
CMyCls* pCls = NULL;
switch (m_byClsFamily)//data type is BYTE
{
case _FAMILY_1_:
switch (dwClsId)
{
case 0x8242:
if (bFe || (dwDId == DB_18C453))
pCls = new CMyCls1;
break;
case 0x8252:
if (bFe || (dwDId == DB_18C452))
pCls = new CMyCls2;
break;
...
...
}
break;
case _FAMILY_2_:
...
case _FAMILY_3_:
...
case _FAMILY_4_:
...
...
break;
}
return pCls;
}[/code]
Here All CMyCls1, CMyCls2, CMyCls3, CMyCls4,... all are derived from a class named "CMyCls".
Any help very thankful to them. It is urgent.


[blue][b][italic]Nice talking to you.[/italic][/b][/blue] :-) ;-)

Comments

  • [b][red]This message was edited by stober at 2005-9-30 8:25:16[/red][/b][hr]
    I think you need to make a couple design changes. derive the base class from CObject and use DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE so that class objects can be easily created at runtime. After that, its just a simple matter of passing to your function a CRuntimeClass pointer to the desired class. Here is a simple example and has a base class and one derived class. you can easily expand this idea to any number of derived classes.

    This is the MyClass.h file
    [code]
    class CMyBaseClass : public CObject
    {
    public:
    DECLARE_DYNCREATE(CMyBaseClass)
    CMyBaseClass();
    virtual ~CMyBaseClass();

    };

    class CMyClass1 : public CMyBaseClass
    {
    public:
    DECLARE_DYNCREATE(CMyClass1)
    CMyClass1();
    virtual ~CMyClass1();

    };
    [/code]

    and the MyClass.cpp fle
    [code]
    #include "MyClass.h"
    IMPLEMENT_DYNCREATE(CMyBaseClass, CObject)
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    CMyBaseClass::CMyBaseClass()
    {

    }

    CMyBaseClass::~CMyBaseClass()
    {

    }


    /// CMyClass1
    IMPLEMENT_DYNCREATE(CMyClass1,CMyBaseClass)
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    CMyClass1::CMyClass1()
    {

    }

    CMyClass1::~CMyClass1()
    {

    }


    CMyBaseClass* foo(CRuntimeClass* pRuntimeClass)
    {
    return (CMyBaseClass*)pRuntimeClass->CreateObject();

    }
    [red]// here is how to call function foo() above[/red]
    void bar()
    {
    CMyClass1* pClass = (CMyClass1*)foo(CRUNTIME_CLASS(CMyClass1));

    [/code]



  • Hello Stober,
    Thankyou vermuch for all your replies. My actual problem is not that. I need to simplify my switch case. I am searching for the easyest way even the cases may be increased in near future. In different case I need to create different Calss's object. Creating object is not a problem. But the switch case becomeing very big. I am looking for any good template or any other solution.
    Thank you. I would like to have your mail id.
    My id is gharanadhgupta@yahoo.co.in, gharanadhgupta@gmail.com


    [blue][b][italic]Nice talking to you.[/italic][/b][/blue] :-) ;-)

  • [b][red]This message was edited by stober at 2005-9-30 14:27:39[/red][/b][hr]
    : Hello Stober,
    : Thankyou vermuch for all your replies. My actual problem is not that. I need to simplify my switch case. I am searching for the easyest way even the cases may be increased in near future. In different case I need to create different Calss's object. Creating object is not a problem. But the switch case becomeing very big. I am looking for any good template or any other solution.
    : Thank you. I would like to have your mail id.
    : My id is gharanadhgupta@yahoo.co.in, gharanadhgupta@gmail.com
    :
    :
    : [blue][b][italic]Nice talking to you.[/italic][/b][/blue] :-) ;-)
    :
    :

    using the method I suggested you can delete that switch altogether. Whatever sets the integer types before calling that function can just as easily set a CRuntimeClass pointer to whatever class is needed using the RUNTIME_CLASS macro.

    delete m_byClsFamily entirely (unless you need it for other reasons) and replace dwClsId with CRuntimeClass*.
    [code]
    CMyCls* CEmr:GetCls(DWORD dwDId, CRuntimeClass* pClass)
    {
    CMyCls* pCls = (CMyCls*)pClass->CreateObject();
    return pCls;
    }
    [/code]

    Whatever calls the above function must already know what class needs to be instantiated otherwise how would it know what integer values to pass. It can just as easily pass RUNTIME_CLLASS(CMyCls1) as an integer that represents the same thing.


    : I would like to have your mail id.
    I don't have one. I think you will find my e-mail address in my personal information link.









  • I think your explanation is clear enough, ... but i still could not realize how this single commande line could make that job !!!!



    : [b][red]This message was edited by stober at 2005-9-30 14:27:39[/red][/b][hr]
    : : Hello Stober,
    : : Thankyou vermuch for all your replies. My actual problem is not that. I need to simplify my switch case. I am searching for the easyest way even the cases may be increased in near future. In different case I need to create different Calss's object. Creating object is not a problem. But the switch case becomeing very big. I am looking for any good template or any other solution.
    : : Thank you. I would like to have your mail id.
    : : My id is gharanadhgupta@yahoo.co.in, gharanadhgupta@gmail.com
    : :
    : :
    : : [blue][b][italic]Nice talking to you.[/italic][/b][/blue] :-) ;-)
    : :
    : :
    :
    : using the method I suggested you can delete that switch altogether. Whatever sets the integer types before calling that function can just as easily set a CRuntimeClass pointer to whatever class is needed using the RUNTIME_CLASS macro.
    :
    : delete m_byClsFamily entirely (unless you need it for other reasons) and replace dwClsId with CRuntimeClass*.
    : [code]
    : CMyCls* CEmr:GetCls(DWORD dwDId, CRuntimeClass* pClass)
    : {
    : CMyCls* pCls = (CMyCls*)pClass->CreateObject();
    : return pCls;
    : }
    : [/code]
    :
    : Whatever calls the above function must already know what class needs to be instantiated otherwise how would it know what integer values to pass. It can just as easily pass RUNTIME_CLLASS(CMyCls1) as an integer that represents the same thing.
    :
    :
    : : I would like to have your mail id.
    : I don't have one. I think you will find my e-mail address in my personal information link.
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :

  • : I think your explanation is clear enough, ... but i still could not realize how this single commande line could make that job !!!!
    :

    That is the standard way MFC uses to dynamically create classes at runtime. You can do the same thing with your own classes. Read MSDN about CObject class, RUNTIME_CLASS macro, and CObject->CreateClass() method.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories