Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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; } 
like image 478
q0987 Avatar asked Mar 31 '11 12:03

q0987


People also ask

How can we define pointer to static functions?

C++ Pointers to members Pointers to static member functions A static member function is just like an ordinary C/C++ function, except with scope: It is inside a class , so it needs its name decorated with the class name; It has accessibility, with public , protected or private .

Can we use this pointer in static member function?

'this' pointer is not available in static member functions as static member functions can be called without any object (with class name).

How do you call a pointer to a member function?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

How do you define a pointer to a class member?

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures. Also as with all pointers, you must initialize the pointer before using it.


2 Answers

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; 
like image 176
Xeo Avatar answered Oct 08 '22 12:10

Xeo


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

like image 30
Konstantin Burlachenko Avatar answered Oct 08 '22 12:10

Konstantin Burlachenko