如何定义指向静态成员函数的函数指针?

时间:2011-03-31 12:47:29

标签: c++

#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来指向静态成员函数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;
}

4 个答案:

答案 0 :(得分:35)

指向静态成员函数的指针只是一个普通的函数指针。 typedef void (*PPMF2)()。您将它分配给静态成员函数,就像分配任何函数指针一样,只是静态成员函数在类范围内:

PPMF2 myfunc = &MyClass::StaticMemberFunc;

答案 1 :(得分:6)

关于静态成员函数保证:

С++ 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)()

答案 2 :(得分:1)

如果函数是静态的,则不需要调用(隐式)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;
}

编辑:

从typedef中删除了违规的静态。

答案 3 :(得分: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();
}

有关详细信息,请参阅link