#include <stdio.h>
#include <tchar.h>

///////////////////////////////////////////////////////////////////////////////
struct Operand1
{
    int GetOp1()
    {
        return 2;
    }

    int GetOp2()
    {
        return 3;
    }
};

struct Operand2
{
    int GetOp1()
    {
        return 3;
    }

    int GetOp2()
    {
        return 4;
    }
};

///////////////////////////////////////////////////////////////////////////////
template<class T> 
struct CalcEngine1
{
    int Calc(void)
    {
        T* pThis= (T*)this;
        return pThis->GetOp1() / pThis->GetOp2();
    }
};

template<class T> 
struct CalcEngine2
{
    double Calc(void)
    {
        T* pThis= (T*)this;
        return pThis->GetOp1() / (double) pThis->GetOp2();
    }
};

///////////////////////////////////////////////////////////////////////////////
template
<
    class                            OperandPolicy,
    template<class CalcEngine> class CalcEnginePolicy
>
class Calculator : 
    public OperandPolicy,
    public CalcEnginePolicy<OperandPolicy>
{
public:
    void Show (void)
    {
        printf ("Show()=%f\r\n", CalcEnginePolicy<OperandPolicy>::Calc() );
    }
};

///////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
    Calculator <Operand1,CalcEngine1> a;
    Calculator <Operand1,CalcEngine2> b;
    Calculator <Operand2,CalcEngine1> c;
    Calculator <Operand2,CalcEngine2> d;

    a.Show();
    b.Show();
    c.Show();
    d.Show();

    return 0;
}

Posted by 셈말짓기 :