q0987
q0987

Reputation: 35982

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

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

#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;
}

Upvotes: 37

Views: 45223

Answers (4)

Xeo
Xeo

Reputation: 131789

A pointer to a static member function is just a normal function pointer. typedef void (*PPSF)(). 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:

PPSF myfunc = &MyClass::StaticMemberFunc;

Upvotes: 47

debapritam chakra
debapritam chakra

Reputation: 1

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

Upvotes: 0

Konstantin Burlachenko
Konstantin Burlachenko

Reputation: 5665

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

Upvotes: 7

hkaiser
hkaiser

Reputation: 11521

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.

Upvotes: 2

Related Questions