2009. 5. 21. 20:45

call class member function as if global function

바람직한 방법인 것 같지는 않지만 MFC로 만들어지 ActiveX에서 event를 전역함수 처럼 호출하여 쓰는 방법이 있다. 비단 event 뿐 아니라 COleControl를 상속받은 class의 모든 member 함수를 호출할 수 있다.



void AfxFireFunc( CWnd * sender,
                     ONE one,
                     TWO two )
{
    class CHelperCtrl : public CxxxCtrl
    {
    public:
        CHelperCtrl() {}
        void FireCallFunc( ONE one,   TWO two )
        {
            CxxxCtrl::FireFunc( one, two );
        }
    };

    CHelperCtrl * ctrl;
    ctrl = (CHelperCtrl * )s_Controls.Query( sender );

    if (ctrl)
        ctrl->FireFunc( one, two );
}

//
static COleInstances s_Controls;

s_Controls.Query 는 COleControl의 pointer를 가지고 있으며, sender로 자식 창의 pointer를 넣어주면 부모인 COleControl의 pointer를 넘겨준다.