What you have there is a function pointer. But not a normal one, a member function pointer. There a few things different in member function pointers when compared to normal ones.
1. It states where the function comes from (which class/ struct holds it).
In your example void(M :: *pf(int, int)=&M :: set_xy;
, M
is the class which contains the function. So because of this, we state that we are storing a pointer of a member function which is from class M
. Hence the M :: *pf
part.
2. You provide the address of the function to the pointer.
C++ does not allow you to just pass a function to the pointer like in normal functions. For member function pointers we need to pass in the address of it. Thats the reason for the &
operator in =&M :: set_xy;
.
3. You need a instantiated object variable to call the function.
For function pointers, it needs the current state of the parent object in order to call it. Here what happens is something like we use the pointer as the function name when calling. That’s why we use this: (op->*pf)(30,40);
. The reason why we dereference is because the member function pointer contains the address of the function, not the function pointer itself. By dereferencing we get the function pointer.
solved What does void (M :: *pf)(int,int) = &M :: set_xy; do? [closed]