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

前端 未结 4 1506
北海茫月
北海茫月 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 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.

提交回复
热议问题