特别注意代码中的:
&CFunction::
void (CFunction::*ptrFunc[4])();
(this->*ptrFunc[i])();
c->UseFunction();//只能用指针调用,用.调用会出错
#include <iostream>
typedef void (*PTRFunction)(void *);//还不清楚在类里面怎么用
class CFunction
{
public:
CFunction(void)
{
ptrFunc[0] = &CFunction::functionA;
ptrFunc[1] = &CFunction::functionB;
ptrFunc[2] = &CFunction::functionC;
ptrFunc[3] = &CFunction::functionD;
}
~CFunction(void);
public:
void (CFunction::*ptrFunc[4])();
void UseFunction()
{
for (int i=0;i<4;i++)
{
(this->*ptrFunc[i])();
}
}
public:
void functionA(void)
{
std::cout<<"functionA"<<std::endl;
}
void functionB(void){
std::cout<<"functionB"<<std::endl;
}
void functionC(void){
std::cout<<"functionC"<<std::endl;
}
void functionD(void){
std::cout<<"functionD"<<std::endl;
}
};
int main()
{
CFunction *c = new CFunction();
c->UseFunction();
system("pause");
return 0;
}