#include<stdio.h>
int max(int x,int y){
return
(x>y?x:y);}
int main()
{
int
(*ptr)(
int
,
int
);
int a,b,c;
ptr =max;
scanf
(
"%d%d"
,&a,&b);
c=(*ptr)(a,b);
printf
(
"a=%d,b=%d,max=%d"
,a,b,c);
}
|
应该是这样的:
1.定义函数指针类型:
typedef int (*fun_ptr)(int,int);
2.声明
变量,赋值:
fun_ptr max_func=max;
也就是说,赋给函数指针的函数应该和函数指针所指的函数原型是一致的。
列子:
class SGDCmdInfo
{
public:
CUMPImpl::PFN_GDCommand m_pfnCmd;
}
class CUMPImpl
{
public:
typedef bool (CUMPImpl::*PFN_GDCommand)(std::vector<std::string>& strParam , int a );
long Execute(const char* szCmd ,int a);
private:
bool ZMD(std::vector<std::string>& strParam , int a );
bool RegisterCmd(std::string strCmd ,CUMPImpl::PFN_GDCommand pfnCmd);
}
bool RegisterCmd(std::string strCmd ,CUMPImpl::PFN_GDCommand pfnCmd)
{
SGDCmdInfo stInfo;
stInfo.m_pfnCmd = pfnCmd;
m_mapGDCmd[strCmd] = stInfo;
return true;
}
long CUMPImpl::Execute(const char *szCmd , int a)
{
std::vector<std::string> strParam = //这里把字符串转为vector;
std::string& strCmdName = strParam[0];
std::map<std::string ,SGDCmdInfo> ::const_iterator citr = m_mapGDCmd.find(strCmdName);
if( citr ==m_mapGDCmd.end())
{
return -1;
}
const SGDCmdInfo& info = citr->second;
bool bRet =(this->*info.m_pfnCmd)(strParam , req);
if( !bRet)
{
return -1;
}
return 0;
}
bool CUMPImpl::ZMD(std::vector<std::string>& strParam , int a )
{
int a = atoi(strParam[0].c_str());
int b = atoi(strParam[1].c_str());
return true;
}
//用法
void Init()
{
RegisterCmd( "
ZMD" , &
CUMPImpl::
ZMD );
//注册
return true;
}
//游戏中GD后台命令
/GM指令应用
std::vector<std::string>::ExtractString(const std::string& sSource , char delim , bool bEnableEmpty) //字符串分割
{
std::string::size_type pose1 , pose2;
std::vector<std::string> strParams;
pos2 = 0;
while( pos2 != std::string::npos )
{
if(bEnableEmpty)
{
if( sSource[pos2] == delim )
{
pos2+=1;
strParams.push_back("");
continue;
}
}
pos1 = sSource.find_first_not_of(delim,pos2);
if( pos1 == std::string::npos )
{
break;
}
pos2 = sSource.find_fist_of(delim , pos1+1 );
if( pos2 == std::string::npos)
{
if( pos1 != sSource.size() )
strParams.push_back(sSource.substr(pos1) );
break;
}
strParams.push_back(sSource.substr(pos1,pos2-pos1));
pos2+=1;
}
if(bEnableEmpty)
{
if(sSource.size() >= 1 && sSource[sSource.size() -1 ] == delim )
{
strParams.push_back("");
}
}
return strParams;
}
class CGMMgrImpl
{
public:
typedef bool (CGMMgrImpl::*PFN_GMCommand)(std::vector<std::string>&strParams , CRole *pRole); //函数指针
CGMMgrImpl();
~CGMMgrImpl();
bool Init();
bool Execute(const char* szCmd , CRole *pRole);
private:
bool ShowVersion( std::vector<std::string> &strParams , CRole *pRole );
bool RegisterCmd(std::string strCmd , int nLevel , CGMMgrImpl::PFN_GMCommand pfnCmd);
}
//执行函数
bool CGMMgrImpl::ShowVersion( std::vector<std::string> &strParams , CRole *pRole )
{
char version[256];
sprintf(version , "GameServer Build Time: %s :%s\n" ,__DATE__,__TIME__);
pRole->SendMessage(version);//发消息给客户端
pRole->SendMessage(VERSION);
return true;
}
//注册
bool CGMMgrImpl::RegisterCmd(std::string strCmd , int nLevel , CGMMgrImpl::PFN_GMCommand pfnCmd)
{
SGMCmdInfo stInfo;
stInfo.m_nLevel = nLevel;
stInfo.m_pfnCmd = pfnCmd;
m_mapGMCmd[strCmd] = stInfo;
return true;
}
//初始化
bool CGMMgrImpl::Init()
{
if(RegisterCmd("ShowVersion" ,1 , &CGMMgrImpl::ShowVersion)){return false ;}
return true;
}
bool CGMMgrImpl::Execute(const char* szCmd , CRole *pRole)
{
std::vector<std::string> strParams = ExtractString(szCmd , '');
if( strParams.size() == 0)
{
return false;
}
std::string& strCmdName = strParams[0];
std::map<std::string , SGMCmdInfo>::const_iterator citr = m_mapGMCmd.find(strCmdName);
if( citr == m_mapGMCmd.end)
{
rreturn false;
}
const SGMCmdInfo& info = citr->second;
bool bRet = (this->*info.m_pfnCmd)(strParams , pRole); //回调函数执行
return bRet;
}
class SGMCmdInfo
{
public:
int m_nLevel;
CGMMgrImpl::PFN_GMCommand m_pfnCmd;
}