0

I have a

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     ;
};

from which derives a

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 ) ;
};

and I would like the methods

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

and

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

to do things accordingly to the runtime type of pPAYOFF, but without resorting to a

dynamic_cast<>

Typically,

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

would do something like

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

and

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

where CALL_TOYFD and PUT_TOYFD are public derived from PAYOFF_TOYFD. And after, I would like to be able to write something like

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

where bc is an instance of BC_TOYFD, and where pPAYOFF is a pointer to PAYOFF_TOYFD, such that the right types for bc and pPAYOFF are resolved at runtime.

I have been told to use "double dispatch" or "reverse double dispatch" pattern for, without any other hint/precision. I have tried to implement it in this framework, without really knowing how to do it exactly. By the way, I will have "other" classes like DIRICHLET_TOYFD deriving from BC_TOYFD, for which I will have to save the same problem that the one I am trying to solve, so that I guess that the double dispatch put in practice in my case will have to take this constraint into account.

Any help would be appreciated !

Thanks a lot !

Olórin
  • 3,367
  • 2
  • 22
  • 42

1 Answers1

0

Ok, after realizing being completely ignorant in the involved patterns, and after a bit of learning, I finally understood the dispatch pattern, and that the visitor pattern was a double dispatch pattern indeed. ;-) Concerning the code snippets I proposed, here is the solution :

// 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;
}

doing indeed what I expect it to do

Olórin
  • 3,367
  • 2
  • 22
  • 42