#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; }
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 .
'this' pointer is not available in static member functions as static member functions can be called without any object (with class name).
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);
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.
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;
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)()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With