C++类之间函数回调的使用

//  Callback.h

#if !defined(CALLBACK_H)
#define CALLBACK_H


class cCallback
{
 public:
  virtual bool Execute(void *Param) const =0;
};


template <class cInstance>
class TCallback : public cCallback // Inheriting
{
 public:
  TCallback() // constructor
  {
   // Important : zeroing the pointer so we later can check for errors
   pFunction = 0;
  }

  // (You can change the callback to take more parameters or to return something)
  typedef bool (cInstance::*tFunction)(void *Param);
  
  // Execute the Callback
  virtual bool Execute(void *Param) const
  {
   if (pFunction) return (cInst->*pFunction)(Param);
   else printf("ERROR : the callback function has not been defined !!!!");
   // an undefined callback function will crash the program if you don't check here !
   return false;
  }

  // Set or change the Callback
  void SetCallback (cInstance  *cInstancePointer,
        tFunction   pFunctionPointer)
  {
   cInst     = cInstancePointer;
   pFunction = pFunctionPointer;
  }

 private:
  cInstance  *cInst;
  tFunction  pFunction;
};

#endif // CALLBACK_H

 


-------------------------------------------------------

#pragma once
#include "CallBack.h"


class CWork
{
public:
 CWork(void);
 ~CWork(void);
 bool CallbackOutput(void *Param);

 TCallback<CWork> Str_CallbackOutput;
 
};
------------------

#include "stdafx.h"
#include "Work.h"

CWork::CWork(void)
{
 Str_CallbackOutput.SetCallback(this, &CWork::CallbackOutput);
}

CWork::~CWork(void)
{
}


bool CWork::CallbackOutput(void *Param)
{
 char s8_Out[200];
 sprintf(s8_Out, "  ***** Some stars *****  %s/n", (char*) Param);

 printf(s8_Out);
 return true;
}

-----

int InitAndConnet(HWND hwnd,unsigned int WM_WINSOCK,UINT port,CString ip,cCallback *p_CallbackOutput);

cCallback *m_CallbackOutput;

 

--


int CMySocketClient::InitAndConnet(HWND hwnd,unsigned int WM_WINSOCK,UINT port,CString ip,cCallback *p_CallbackOutput)

 m_CallbackOutput=p_CallbackOutput;
 if (this->InitSocket()!=0)
 {      
        return -1;
 }
 
 m_hWnd=hwnd;
 m_uPort=port;
 m_ip=ip;
 m_WM_WINSOCK=WM_WINSOCK;

 
 SOCKET m_Socket = socket(AF_INET,SOCK_STREAM,0);
 m_hSocket=m_Socket;

 //准备服务器的信息,这里需要指定服务器的地址
 m_hostAddr.sin_addr.S_un.S_addr = inet_addr(m_ip); //inet_addr("127.0.0.1");
 m_hostAddr.sin_family = AF_INET;
 m_hostAddr.sin_port = htons(m_uPort);
 
 //这里主动连接服务器,该过程将等待一定时间 
 int ret =connect(m_hSocket,(sockaddr*)&m_hostAddr,sizeof(sockaddr));


 if(ret == SOCKET_ERROR)
 {//连接失败
  if(GetLastError()!=WSAEWOULDBLOCK)
  {
   AfxMessageBox(_T("请确认服务器确实已经打开并工作在同样的端口!"));
   return -1;
  }
 }


 if(WSAAsyncSelect(m_hSocket,m_hWnd,m_WM_WINSOCK,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
 {
  return -1;//AfxMessageBox("Error in select");
 }
 return 0;

}

//
//int CMySocketClient::InitWSAAsyncSelect()
//{
// if(WSAAsyncSelect(m_hSocket,m_hWnd,m_WM_WINSOCK,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
// {
//  return -1 ;//AfxMessageBox("Error in select");
// }
// return 0;
//}

 

 

int CMySocketClient::SendString(CString str)
{
 if(send(m_hSocket,str.GetBuffer(0),str.GetLength(),0)==SOCKET_ERROR)
 {
  return -1 ; //AfxMessageBox("Client Send data error");
 }
 return 0;
}

 

 

m_CallbackOutput->Execute((void*)TempPackBytes);

 

 

-------------------------

 CWork *pwork=new CWork();

 m_aa=100;
    //m_MySocketClient.InitAndConnet(m_hWnd,WM_CLIENT_READCOLOSE,8888,"127.0.0.1"); 
 m_MySocketClient.InitAndConnet(m_hWnd,WM_CLIENT_READCOLOSE,8888,"127.0.0.1",&pwork->Str_CallbackOutput); 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值