微网站开发入门浙江建站管理系统价格
web/
2025/9/29 4:29:22/
文章来源:
微网站开发入门,浙江建站管理系统价格,wordpress head文件,专业的网站建设平台前篇文章解决了关于函数指针的不同类成员函数传递的问题。不知道细心的朋友看见我上篇文章后#xff0c;是否发现参数必须被限制为特定的参数。
原来改进的代码如下#xff1a; class CCallFuctionList { public:CCallFuctionList(); virtual ~CCallFuctionL…前篇文章解决了关于函数指针的不同类成员函数传递的问题。不知道细心的朋友看见我上篇文章后是否发现参数必须被限制为特定的参数。
原来改进的代码如下 class CCallFuctionList
{
public:
CCallFuctionList();
virtual ~CCallFuctionList(void);
typedef boost::functionvoid() FUNCPTR;
typedef std::multimapunsigned int, FUNCPTR FUNCMAP;
typedef std::multimapunsigned int, FUNCPTR::iterator FUNCMAP_ITERATOR;
void CallDoRegister();
private:
FUNCMAP_ITERATOR m_funcIter;
int i;
FUNCMAP m_funcMap;
};
以FUNCPTR作为参数能传递的函数形式只能为void xxx(void)这种类型的。
例如#define REGISTE_FUNC(type, func) m_FuncMap.insert(std::make_pair(type, func)) REGISTE_FUNC(SCMD_REGISTER, boost::bind(CCallFuctionList::CallDoRegister, this));
即既FUNCPTR参数只能接受void CallDoRegister()这种类似的类成员函数
那么有没有办法可以解决不同参数类型的函数成员呢答案当然是有。 我们可以模仿MFC里面的回调函数做法。具体修改如下 enum
{
voidFunV,
intFunV,
intunI
}FuncType;
union pFunc
{
boost::functionvoid() pVoidFunV;
boost::functionint() pIntFunV;
boost::functionint(int) pVoidFunI;
};
class CCallFuctionList
{
public:
#define REGISTE_FUNC(type, func) m_FuncMap.insert(std::make_pair(type, func))
CCallFuctionList();
virtual ~CCallFuctionList(void);
typedef boost::functionvoid() FUNCPTR;
typedef std::mapunsigned int, pFunc FUNCMAP;
typedef std::mapunsigned int, pFunc::iterator FUNCMAP_ITERATOR;
void CallDoRegister();
void InsertMapFunc(int ID, FUNCPTR f);
void Run(int ID, FuncType fType);
private:
FUNCMAP_ITERATOR m_funcIter;
int i;
FUNCMAP m_funcMap;
FuncType m_FuncType;
pFunc m_pFunc;
};
void CCallFuctionList::InsertMapFunc(int ID, FUNCPTR f)
{
m_pFunc.pVoidFunV f;
REGISTE_FUNC(ID, m_pFunc);
}
void CCallFuctionList::Run(int ID, FuncType fType, int a 0);
{
m_pFunc m_funcMap[ID];
switch(fType)
{
case voidFunV:
(this-*m_pFunc.pVoidFunV)(void);
break;
case intFunV:
(this-*m_pFunc.pIntFunV)(void);
break;
case intFunI:
(this-*m_pFunc.pVoidFunI)(a);
break;
}
} 具体调用如下 class A
{
public:
A()
{
m_funcList.InsertMapFunc(SCMD_REGISTER, (CCallFuctionList::FUNCPTR)boost::bind(A::Insert, this));
m_funcList.InsertMapFunc(SCMD_LOGIN, (CCallFuctionList::FUNCPTR)boost::bind(A::InsertA, this));
}
void Run()
{
m_funcList.Run(SCMD_REGISTER, intFunI, 5);
m_funcList.Run(SCMD_LOGIN, intFunv);
}
private:
int Insert(int);
int InsertA();
private:
CCallFuctionList m_funcList;
} 通过两张表一张参数类型表一张函数指针表来实现参数与函数指针的对应。MFC具有借鉴意义。
当然不排除有更好的方法如果有更好的办法欢迎指教
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83700.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!