/*****************************************************************************
FileName : testTstPf.cpp
FileFunc : VC++类中函数指针的使用
Version : V0.1
Author : from jernymy
Date : 2011-09-03
Descp : VC++ 的类中使用函数指针的小例子,通过函数指针调用不同的函数
*****************************************************************************/
#include <stdio.h>
class TstPf
{
public:
TstPf(void);
~TstPf(void);
void TstA(void);
void PrtChar(void *pvData);
void PrtInt (void *pvData);
void PrtStr (void *pvData);
// for every env call method
void (TstPf::*pfPrt) (void *pvData);
};
TstPf::TstPf(void)
{
this->pfPrt = NULL;
}
TstPf::~TstPf(void)
{
this->pfPrt = NULL;
}
void TstPf::TstA(void)
{
this->pfPrt = &TstPf::PrtChar; // jernymy 指向函数地址
(this->*(pfPrt))((void *)'C'); // jernymy 调用时的用法
this->pfPrt = &TstPf::PrtInt;
(this->*(pfPrt))((void *)201107424);
this->pfPrt = &TstPf::PrtStr;
(this->*(pfPrt))((void *)"jernymy function pointer used in class");
}
void TstPf::PrtChar(void *pvData)
{
char chVar = (char)pvData;
printf("%s:%d run\n", __FILE__, __LINE__);
printf("chVar:%c\n", chVar);
}
void TstPf::PrtInt(void *pvData)
{
int nVar = (int)pvData;
printf("%s:%d run\n", __FILE__, __LINE__);
printf("nVar:%d\n", nVar);
}
void TstPf::PrtStr(void *pvData)
{
char *lpVar = (char *)pvData;
printf("%s:%d run\n", __FILE__, __LINE__);
printf("lpVar:%s\n", lpVar);
}
int main(void)
{
TstPf cA;
cA.TstA();
return 0;
}
VC++ 的类中使用函数指针的小例子,通过函数指针调用不同的函数
最新推荐文章于 2022-08-11 10:55:51 发布