1. 一般函数的指针定义
int foo(const char *out)
{
cout << "foo is called: " << out << endl;
return strlen(out);
}
typedef int (FooFunType)(const char*); // 函数的类型别名
typedef int (FooFunPointerType)(const char*); // 函数指针类型的别名
FooFunType* f1 = foo;
FooFunPointerType f2 = foo;
f1("f1-FooFunType");
f2("f2-FooFunPointerType");
// 输出为:
// foo is called: f1-FooFunType
// foo is called: f2-FooFunPointerType
2. 成员函数指针的定义
class Foo
{
public:
int Print(const char *out)
{
cout << "Foo::Print is called: " << out << endl;
}
};
typedef int (Foo::*FooMemPtr)(const char *out);
FooMemPtr clbkFooPrint = &Foo::Print;
Foo foo;
Foo *pFooObj = &foo;
pFooObj->*clbkFooPrint("class member function called.");