C++ 如何定义指向静态成员函数的函数指针?
问题:我需要定义一个指向静态成员函数verifyAddress2的函数指针PPMF2。我怎么做C++ 如何定义指向静态成员函数的函数指针?,c++,C++,问题:我需要定义一个指向静态成员函数verifyAddress2的函数指针PPMF2。我怎么做 #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 PPM
#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;
}
指向静态成员函数的指针只是普通函数指针<代码>类型定义无效(*PPMF2)()。您可以像分配任何函数指针一样将其分配给静态成员函数,只要静态成员函数在类范围内:
#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;
}
如果函数是静态的,则不需要调用(隐式)
此
指针。因此,指向静态成员函数的指针与成员函数指针不同:
PPMF2 myfunc = &MyClass::StaticMemberFunc;
编辑:
从typedef中删除了有问题的static。关于静态成员函数保证: С++ISO/IEC 14882 2003-10-15规定 5.2.2函数调用有两种:普通函数调用和成员函数57)(9.3)调用 57)静态成员函数(9.4)是普通函数 理论上,静态成员函数可以有另一个调用约定。 但是standart允许我们利用这些东西 答复: typedef void(Person::*PPMF2)(=>typedef void(*PPMF2)()包括 使用名称空间std; 甲级 { 私人: int x,y; 静态int a; 公众: () { x=10; y=11; } ~A() { } void displaynostatic() {
我看不出这个问题被否决的原因。问这个问题是错误的吗?+1来补偿它!^+1-我认为这是一个有效的回答question@Nawaz,PPMF的定义/用法是正确的。但是,我想对静态成员函数执行类似的操作,VS2010给了我在原始问题中更新的错误。在在第三个版本中,您有一个静态函数返回非静态函数的地址。我希望编译器在
static PPMF verificationFUnction()中大惊小怪
您需要一个实例来引用return&Person::verifyAddress;
,因为verifyAddress
不是静态的。@Xeo,它对我不起作用类似的问题:错误C2440:“return”:无法从“void(u cdecl*)(void)转换'到'PPMF2'@q0987:我编译它没有任何问题:Person p;PPMF2 pf=p.verificationFUnction2()
。您是否更改了PPMF2
的typedef
?@Xeo,您使用了哪个版本的VS?我正在使用VS2010。@q0987:相同。VS201010.0.30319.1 RTMRel
@q0987:正如我所说,您必须将PPMF2的typedef更改为typedef void(*PPMF2)()
。静态成员函数与自由函数差别不大,只是作用域不同。将PPMF2
typedef更改为typedef void(*PPMF2)()
是的,我忽略了typedef中的静态,我编辑了答案。这仍然有一个静态函数验证函数
返回一个非静态函数的地址验证地址
,但它不起作用。虽然这个代码片段可以解决这个问题,但确实有助于提高你的文章质量。记住我相信你的静态指针是不正确的,它的类型中不应该有一个::。
#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;
}
#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();
}