#include "stdafx.h"
class Person;
typedef void (Person::*PPMF)();
// error C2159: more than one storage class specified
typedef static void (Person::*PPMF2)();
class Person
{
public:
static PPMF verificationFUnction()
{
return &Person::verifyAddress;
}
// error C2440: 'return' : cannot convert from
// 'void (__cdecl *)(void)' to 'PPMF2'
PPMF2 verificationFUnction2()
{
return &Person::verifyAddress2;
}
private:
void verifyAddress() {}
static void verifyAddress2() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
Person scott;
PPMF pmf = scott.verificationFUnction();
(scott.*pmf)();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题:我需要定义一个函数指针PPMF2来指向一个静态成员函数verifyAddress2.我该怎么做?
#include "stdafx.h"
class Person;
typedef void (Person::*PPMF)();
typedef void (Person::*PPMF2)();
class Person
{
public:
static PPMF verificationFUnction()
{
return &Person::verifyAddress;
}
PPMF2 verificationFUnction2()
{
return &Person::verifyAddress2;
}
private:
void verifyAddress() {}
static void verifyAddress2() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
Person scott;
PPMF pmf = scott.verificationFUnction();
(scott.*pmf)();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Xeo*_*Xeo 36
指向静态成员函数的指针只是一个普通的函数指针.typedef void (*PPMF2)().您可以将它分配给静态成员函数,就像分配任何函数指针一样,只是静态成员函数在类范围内:
PPMF2 myfunc = &MyClass::StaticMemberFunc;
Run Code Online (Sandbox Code Playgroud)
关于静态成员函数保证:
С++ ISO/IEC 14882 2003-10-15说
5.2.2函数调用有两种:普通函数调用和成员函数57)(9.3)调用....
57)静态成员函数(9.4)是普通函数.
理论上,static-member-functions可以有另一个调用约定.但标准允许我们利用这样的事情......
答案:typedef void(Person ::*PPMF2)()=> typedef void(*PPMF2)()