C++闭源接口的二次封装
Posted 华仔Ivan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++闭源接口的二次封装相关的知识,希望对你有一定的参考价值。
dofun no para
D::funC3 no para
dofun,a=10
D::funC3,a=10
#include "stdafx.h"
#include <stdlib.h>
struct A
{
virtual void funA1()=NULL;
virtual void funA2()=NULL;
};
struct B:A
{
virtual void funB1() = NULL;
virtual void funB2() = NULL;
};
struct C:B
{
virtual void funC1() = NULL;
virtual void funC2() = NULL;
virtual void funC3() = NULL;
virtual void funC3(int a) = NULL;
};
struct D :C
{
virtual void funA1(){ printf("%s\\n", __FUNCTION__); };
virtual void funA2(){ printf("%s\\n", __FUNCTION__); };
virtual void funB1(){ printf("%s\\n", __FUNCTION__); };
virtual void funB2() { printf("%s\\n", __FUNCTION__); };
virtual void funC1(){ printf("%s\\n", __FUNCTION__); };
virtual void funC2() { printf("%s\\n", __FUNCTION__); };
virtual void funC3() { printf("%s no para\\n", __FUNCTION__); };
virtual void funC3(int a) { printf("%s,a=%d\\n", __FUNCTION__,a); };
};
// C++闭源接口的二次封装
template<class T>
void dofun(T *obp, void(T::*p)())
{
printf("%s no para\\n", __FUNCTION__);
(obp->*p)();
}
template<class T>
void dofun(T *obp, int a, void(T::*p)(int))
{
printf("%s,a=%d\\n", __FUNCTION__,a);
(obp->*p)(a);
}
int _tmain(int argc, _TCHAR* argv[])
{
C* d=new D;
//d->funC3();
//d->funC3(10);
dofun(d, &C::funC3);
dofun(d, 10, &C::funC3);
delete d;
d = NULL;
system("pause");
return 0;
}
应用例子:
//登录完成
CMD_GP_LogonFinish LogonFinish;
ZeroMemory(&LogonFinish,sizeof(LogonFinish));
LogonFinish.wIntermitTime=m_pInitParameter->m_wIntermitTime;
LogonFinish.wOnLineCountTime=m_pInitParameter->m_wOnLineCountTime;
//m_pITCPNetworkEngine->SendData(dwContextID,MDM_GP_LOGON,SUB_GP_LOGON_FINISH,&LogonFinish,sizeof(LogonFinish));
doSendData(m_pITCPNetworkEngine,&ITCPNetworkEngine::SendData,dwContextID,MDM_GP_LOGON,SUB_GP_LOGON_FINISH,&LogonFinish,sizeof(LogonFinish));
// C++闭源接口的二次封装
template<class T>
bool doSendData(T *obp,bool(T::*p)(DWORD, WORD, WORD),DWORD dwSocketID, WORD wMainCmdID, WORD wSubCmdID)
{
bool bret=(obp->*p)(dwSocketID, wMainCmdID, wSubCmdID);
//构造提示
TCHAR szDescribe[128]=TEXT("");
_sntprintf(szDescribe,CountArray(szDescribe),TEXT("bret=%d,wMainCmdID=%d,wSubCmdID=%d,dwSocketID=%d"),
bret,wMainCmdID,wSubCmdID,dwSocketID);
//提示消息
CTraceService::TraceString(szDescribe,TraceLevel_Normal);
return bret;
}
template<class T>
bool doSendData(T *obp, bool(T::*p)(DWORD, WORD, WORD, VOID *, WORD), DWORD dwSocketID, WORD wMainCmdID, WORD wSubCmdID, VOID * pData, WORD wDataSize)
{
bool bret=(obp->*p)(dwSocketID, wMainCmdID, wSubCmdID, pData,wDataSize);
//构造提示
TCHAR szDescribe[128]=TEXT("");
_sntprintf(szDescribe,CountArray(szDescribe),TEXT("bret=%d,wMainCmdID=%d,wSubCmdID=%d,wDataSize=%d,dwSocketID=%d"),
bret,wMainCmdID,wSubCmdID,wDataSize,dwSocketID);
//提示消息
CTraceService::TraceString(szDescribe,TraceLevel_Normal);
return bret;
}
以上是关于C++闭源接口的二次封装的主要内容,如果未能解决你的问题,请参考以下文章