我有一个
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的正确类型在运行时被解析。
我被告知使用“double dispatch”或“reverse double dispatch”模式,没有任何其他提示/精度。我试图在这个框架中实现它,而不是真正知道如何完成它。顺便说一句,我将有来自BC_TOYFD的“其他”类,如DIRICHLET_TOYFD,为此,我将不得不保存与我试图解决的问题相同的问题,以便我认为在我的情况下实施的双重调度将不得不考虑这一约束。
任何帮助将不胜感激!
非常感谢!
答案 0 :(得分: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;
}
确实做了我期望它做的事情