模拟实现VC中的handle的c++方法,欢迎指正~~~

模拟实现VC中的handle的c++方法,可以使用在类层次结构的设计中。

代码没有添加注解,很简单嘛。程序使用dev c++调试通过。

#include
#include

using namespace std;
typedef void *   HANDLE;

class CTartgetCmd{
public:
        CTartgetCmd();
virtual ~CTartgetCmd();

virtual void  OnCmdMsg(unsigned int nCommand);
HANDLE GetHandle();

protected:
HANDLE m_hHandle;
};


CTartgetCmd::CTartgetCmd()
{
    m_hHandle = this;
}

CTartgetCmd::~CTartgetCmd()
{
    m_hHandle = NULL;
}
void  CTartgetCmd::OnCmdMsg(unsigned int nCommand)
{
}
HANDLE CTartgetCmd::GetHandle()
{
    return m_hHandle;
}
void SendMessage(HANDLE hHandle, unsigned int nCommand)
{
    CTartgetCmd *pCmd = (CTartgetCmd *)(hHandle);
    pCmd->OnCmdMsg(nCommand);
}

class CWnd : public CTartgetCmd
{
public:
 CWnd();
virtual ~CWnd();
 
 virtual void  OnCmdMsg(unsigned int nCommand);
 virtual unsigned int SendMessage(unsigned int nCommand);  
};
CWnd::CWnd()
{
}
CWnd::~CWnd()
{
}

void  CWnd::OnCmdMsg(unsigned int nCommand)
{
    cout<< nCommand <}

unsigned int CWnd::SendMessage(unsigned int nCommand)
{
   ::SendMessage(GetHandle(),nCommand);
    return 0;
}

int main(int argc, char *argv[])
{
  CWnd myWnd,myWnd2;
  cout<  cout<

  for( int i = 0;i< 3;i++)
  {
    myWnd.SendMessage(i);
  }
  system("PAUSE"); 
  return 0;
}

你可能感兴趣的:(c/c++)