C++ 如何定义指向静态成员函数的函数指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5500057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a function pointer pointing to a static member function?
提问by q0987
#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;
}
Question: I need to define a function pointer PPMF2 to pointing to a static member function verifyAddress2. How can I do it?
问题:我需要定义一个函数指针 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;
}
回答by Xeo
A pointer to a static member function is just a normal function pointer. typedef void (*PPMF2)()
. You assign it to a static member function like you assign any function pointer, only that the static member function is inside the class scope:
指向静态成员函数的指针只是一个普通的函数指针。typedef void (*PPMF2)()
. 您可以将它分配给静态成员函数,就像分配任何函数指针一样,只是静态成员函数在类范围内:
PPMF2 myfunc = &MyClass::StaticMemberFunc;
回答by bruziuz
About static member function guarantees:
关于静态成员函数保证:
С++ ISO/IEC 14882 2003-10-15 says that
С++ ISO/IEC 14882 2003-10-15 说
5.2.2 There are two kinds of function call: ordinary function call and member function 57) (9.3) call....
57) A static member function (9.4) is an ordinary function.
5.2.2 函数调用有两种:普通函数调用和成员函数57)(9.3)调用....
57) 静态成员函数 (9.4) 是一个普通函数。
Theoretically static-member-functions can have another calling convention. But standart allow us to leverage on such thing...
理论上静态成员函数可以有另一个调用约定。但是标准允许我们利用这样的事情......
Answer: typedef void (Person::*PPMF2)() => typedef void (*PPMF2)()
答案:typedef void (Person::*PPMF2)() => typedef void (*PPMF2)()
回答by hkaiser
If the function is static it does not require a (implicit) this
pointer to be invoked. Therefore, a pointer to a static member function is not the same as a member function pointer:
如果函数是静态的,则不需要this
调用(隐式)指针。因此,指向静态成员函数的指针与成员函数指针不同:
#include "stdafx.h"
class Person;
typedef void (Person::*PPMF)();
typedef /*static*/ void (*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();
(*pmf)();
return 0;
}
EDIT:
编辑:
removed the offending static from the typedef.
从 typedef 中删除了有问题的静态。
回答by debapritam chakra
#include<iostream>
using namespace std;
class A
{
private:
int x,y;
static int a;
public:
A()
{
x = 10;
y = 11;
}
~A()
{
}
void displayNonStatic()
{
cout<<x<<" "<<y<<endl;
}
void displayStatic()
{
cout<<a<<endl;
}
};
int A::a = 12;
int main()
{
typedef void (A::*NonStatic)(void);
typedef void (A::*Static)(void);
A a1;
NonStatic _nstatic = &A::displayNonStatic ;
Static _static = &A::displayStatic;
// Always make sure that call to the pointer to the member functions is made within the context of the instance.
//Correct way to call the pointer within the context of the instance " a1 " .
(a1.*_nstatic)();
(a1.*_static)();
//Error case given below, the pointer is not called within the context of the instance
// (*_nstatic)(); ->error
// (*_static)(); ->error
getchar();
}
Refer to the linkfor more information.
有关更多信息,请参阅 链接。