本文主要讲述在成员函数中使用本类的函数指针和在类外(主函数等函数中)使用类的成员函数指针。
//tmp.h
class Tmp
{
public:
Tmp();
~Tmp();
void Print(int a);
void AnotherPrint(int b);
void (Tmp::*pfPrint)(int);
};
//tmp.cpp
Tmp::Tmp()
{
pfPrint = NULL;
}
Tmp::~Tmp()
{
}
void Tmp::Print(int a)
{
cout<<a<<endl;
}
void Tmp::AnotherPrint(int b)
{
pfPrint = Print; //成员函数指针赋值
(this->*pfPrint)(b);//在员函数中使用成员函数指针
}
//main.cpp
int main(int argc,char** argv)
{
Tmp tmp;
tmp.Print(100);
tmp.AnotherPrint(100);
Tmp anotherTmp;
anotherTmp.pfPrint =
}