0

我有一个

class BC_TOYFD
{
    public:
        BC_TOYFD( BS_TOYFD * pBS, BC2 dBC2 );
        virtual ~BC_TOYFD( void ) ;
        BS_TOYFD * _pBS ;
        BC2 _dBC2 ;
        double _PDA ; // store price down approximation
        double _PUA ; // store price up approximation
        virtual void COMPUTEBVDOWNFOR( PAYOFF_TOYFD * pPAYOFF, double * attime ) = 0 ;
        virtual void COMPUTEBVUPFOR( PAYOFF_TOYFD * pPAYOFF, double * attime ) = 0     ;
};

从中得出一个

class DIRICHLET_TOYFD : public BC_TOYFD
{
    public:
        DIRICHLET_TOYFD( BS_TOYFD * pBS, BC2 dBC2 ) ;
        ~DIRICHLET_TOYFD( void ) ;
        void COMPUTEBVDOWNFOR( PAYOFF_TOYFD * pPAYOFF, double * attime ) ;
        void COMPUTEBVUPFOR( PAYOFF_TOYFD * pPAYOFF, double * attime ) ;
};

我想要这些方法

void DIRICHLET_TOYFD::COMPUTEBVDOWNFOR( PAYOFF_TOYFD * pPAYOFF, double * attime )

void DIRICHLET_TOYFD::COMPUTEBVUPFOR( PAYOFF_TOYFD * pPAYOFF, double * attime )

根据 pPAYOFF 的运行时类型执行操作,但不诉诸

dynamic_cast<>

通常,

void DIRICHLET_TOYFD::COMPUTEBVDOWNFOR( PAYOFF_TOYFD * pPAYOFF, double * attime )

会做类似的事情

_PUA = something if the runtime type of pPAYOFF (which is an abstract class) is for instance CALL_TOYFD

_PUA = something else if the runtime type of pPAYOFF (which is an abstract class) is for instance PUT_TOYFD

其中 CALL_TOYFD 和 PUT_TOYFD 是从 PAYOFF_TOYFD 派生的公共。之后,我希望能够写出类似的东西

double approx = bc->COMPUTEBVDOWNFOR( pPAYOFF, attime ) ;

其中 bc 是 BC_TOYFD 的一个实例,而 pPAYOFF 是指向 PAYOFF_TOYFD 的指针,这样 bc 和 pPAYOFF 的正确类型在运行时被解析。

我被告知要使用“双重调度”或“反向双重调度”模式,没有任何其他提示/精度。我试图在这个框架中实现它,但并不真正知道如何准确地做到这一点。顺便说一句,我将有“其他”类,如从 BC_TOYFD 派生的 DIRICHLET_TOYFD,为此我将不得不保存与我试图解决的问题相同的问题,因此我猜双分派在我的案例中付诸实践将不得不考虑这个约束。

任何帮助,将不胜感激 !

非常感谢 !

4

1 回答 1

0

好吧,在意识到对所涉及的模式一无所知之后,经过一番学习,我终于明白了调度模式,访问者模式确实是双重调度模式。;-) 关于我提出的代码片段,这里是解决方案:

// fwd decls

class BOUNDARY_CONDITION_DIRICHLET ;
class BOUNDARY_CONDITION_NEUMANN ;

class PAYOFF
{
    public:
        virtual void on_call( BOUNDARY_CONDITION_DIRICHLET * pBOUNDARY_CONDITION_DIRICHLET ) = 0 ;
        virtual void on_call( BOUNDARY_CONDITION_NEUMANN * pBOUNDARY_CONDITION_NEUMANN ) = 0 ;
};

////////////////////////////////////////////////////////

class BOUNDARY_CONDITION_DIRICHLET : public BOUNDARY_CONDITION
{
public:
     void COMPUTE_APPROX( PAYOFF * pPAYOFF )
    {
        pPAYOFF->on_call( this ) ;
    }
     void on_visit( CALL * pCALL )
    {
        std::cout << "The code \"BOUNDARY_CONDITION_DIRICHLET->f(CALL);\" has been executed" << std::endl ;
    }
     void on_visit( PUT * pPUT )
    {
        std::cout << "The code \"BOUNDARY_CONDITION_DIRICHLET->f(PUT);\" has been executed" << std::endl ;
    }
};

////////////////////////////////////////////////////////

class BOUNDARY_CONDITION_NEUMANN : public BOUNDARY_CONDITION
{
    public:
         void COMPUTE_APPROX( PAYOFF * pPAYOFF )
        {
            pPAYOFF->on_call( this ) ;
        }
         void on_visit( CALL * pCALL )
        {
            std::cout << "The code \"BOUNDARY_CONDITION_NEUMANN->f(CALL);\" has been executed" << std::endl ;
        }
         void on_visit( PUT * pPUT )
        {
            std::cout << "The code \"BOUNDARY_CONDITION_NEUMANN->f(PUT);\" has been executed" << std::endl ;
        }
};

////////////////////////////////////////////////////////

class CALL : public PAYOFF
{
    public:
         void on_call ( BOUNDARY_CONDITION_DIRICHLET * pBOUNDARY_CONDITION_DIRICHLET )
        {
            pBOUNDARY_CONDITION_DIRICHLET->on_visit( this ) ;
        }
         void on_call( BOUNDARY_CONDITION_NEUMANN * pBOUNDARY_CONDITION_NEUMANN)
        {
            pBOUNDARY_CONDITION_NEUMANN->on_visit( this ) ;
        }
};

////////////////////////////////////////////////////////

class PUT : public PAYOFF
{
    public:
         void on_call ( BOUNDARY_CONDITION_DIRICHLET * pBOUNDARY_CONDITION_DIRICHLET )
        {
            pBOUNDARY_CONDITION_DIRICHLET->on_visit( this ) ;
        }
         void on_call( BOUNDARY_CONDITION_NEUMANN * pBOUNDARY_CONDITION_NEUMANN )
        {
            pBOUNDARY_CONDITION_NEUMANN->on_visit( this ) ;
        }
};

int _tmain(int argc, _TCHAR* argv[])
{
    BOUNDARY_CONDITION_DIRICHLET dBOUNDARY_CONDITION_DIRICHLET ;
    BOUNDARY_CONDITION_NEUMANN dBOUNDARY_CONDITION_NEUMANN ;
    CALL dCALL ;
    PUT dPUT ;
    BOUNDARY_CONDITION_DIRICHLET * pBOUNDARY_CONDITION_DIRICHLET = &dBOUNDARY_CONDITION_DIRICHLET ;
    BOUNDARY_CONDITION_NEUMANN * pBOUNDARY_CONDITION_NEUMANN = &dBOUNDARY_CONDITION_NEUMANN ;
    CALL * pCALL = &dCALL ;
    PUT * pPUT = &dPUT ;

    BOUNDARY_CONDITION * pBOUNDARY_CONDITION = pBOUNDARY_CONDITION_DIRICHLET ;
    PAYOFF * pPAYOFF = pCALL ;
    pBOUNDARY_CONDITION->COMPUTE_APPROX( pPAYOFF ) ;

    pBOUNDARY_CONDITION = pBOUNDARY_CONDITION_DIRICHLET ;
    pPAYOFF = pPUT ;
    pBOUNDARY_CONDITION->COMPUTE_APPROX( pPAYOFF ) ;

    pBOUNDARY_CONDITION = pBOUNDARY_CONDITION_NEUMANN ;
    pPAYOFF = pCALL ;
    pBOUNDARY_CONDITION->COMPUTE_APPROX( pPAYOFF ) ;

    pBOUNDARY_CONDITION = pBOUNDARY_CONDITION_NEUMANN ;
    pPAYOFF = pPUT ;
    pBOUNDARY_CONDITION->COMPUTE_APPROX( pPAYOFF ) ;

    system("pause") ;
    return 0;
}

确实在做我期望它做的事情

于 2013-08-14T23:35:49.823 回答