- 指向函数的指针
函数的类型由它的返回值和参数列表决定, 但函数不能返回一个函数类型。
int fce( const char*, ... );
int fc( const char* );
// point to fce
typedef int (*PFCE)( const char*, ... );
// point to fc
typedef int (*PFC)( const char* );
// Initialization
PFCE pfce = fce;
PFC pfc = fc;
// Invoke the functions
pfce();
pfc();
- 指向重载函数的指针
在两个函数指针类型之间不能进行类型转换, 必须严格匹配才能找到相应的函数。
void ff( const char* );
void ff( unsigned int );
// Error: No Match, not available parameter list
void (*pf)(int) = ff;
// Error: No Match, not available return type
double (*pf2)( const char* )) = ff;
- 指向类实例成员函数的指针
指向类实例成员函数的指针必须匹配三个方面:参数类型和个数,返回类型,所属类类型。
class Screen
{
public:
int Add(int lhs, int rhs);
int Del( int lhs, int rhs );
void ShowNumber(int value);
};
typedef int (Screen::*OPE)( int, int);
typedef void (Screen::*SHOW)( int);
OPE ope = &Screen::Add;
SHOW show = &Screen::ShowNumber;
Screen scr;
// Invoke its own member
int value = scr.Add(10, 20);
scr.ShowNumber(value);
// Invoke the function point
value = (scr.*ope)(10, 20);
(scr.*show)(value);
- 指向类静态成员函数的指针
指向类静态成员函数的指针属于普通指针。
class Screen
{
public:
static int Add(int lhs, int rhs);
};
typedef int (*OPE)(int, int);
OPE ope = &Screen::Add;
int value = ope(10, 20);