How to define a function pointer pointing to a static member function?

前端 未结 4 1472
北海茫月
北海茫月 2021-01-17 07:49
#include \"stdafx.h\"

class Person;
typedef void (Person::*PPMF)();

// error C2159: more than one storage class specified
typedef static void (Person::*PPMF2)();           


        
相关标签:
4条回答
  • 2021-01-17 07:58

    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:

    PPMF2 myfunc = &MyClass::StaticMemberFunc;
    
    0 讨论(0)
  • 2021-01-17 08:06
    #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 link for more information.

    0 讨论(0)
  • 2021-01-17 08:11

    About static member function guarantees:

    С++ ISO/IEC 14882 2003-10-15 says that

    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.

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

    0 讨论(0)
  • 2021-01-17 08:13

    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:

    #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.

    0 讨论(0)
提交回复
热议问题