One, the use of member function pointers
In C + +, a pointer to a member function is a very special thing. For ordinary function pointers, it can be treated as an address and can be arbitrarily converted and invoked when needed. However, for member functions, regular type conversions are not compiled, and special syntax must be used when calling. C + + specifically prepares three operators for member pointers: "::*" for pointers, whereas "->*" and ". *" are used to call functions that the pointer points to. Like what:
class tt
{
public: void foo(int x){ printf("\n%d\n",x); }
};
typedef void ( tt::* FUNCTYPE)(int );
FUNCTYPE ptr = tt::foo; //给一个成员函数指针赋值.
tt a;
(a.*ptr)(5); //调用成员函数指针.
tt *b = new tt;
(b->*ptr)(6); //调用成员函数指针.
Note the use of parentheses when calling function pointers, because. * and->* have a lower priority, they must be combined with the elements on both sides of the parentheses inside, otherwise not pass compilation. Not only that, but more importantly, you can't do any type conversion for the member function pointer, such as you want to save the address of a member function to an integer (that is, the address of the class member function), which is not possible by the normal type conversion method. The following code:
DWORD dwFooAddrPtr= 0;
dwFooAddrPtr = (DWORD) &tt::foo; /* Error C2440 */
dwFooAddrPtr = reinterpret_cast (&tt::foo); /* Error C2440 */
You get just two c2440 mistakes. Of course you can't convert a member function type to any of the other slightly different types, simply put, each member function pointer is a unique type and cannot be converted to any other type. Even if the definitions of two classes are identical, they cannot be converted between their corresponding member function pointers. This is somewhat analogous to the type of the struct, each of which is the only type, but the difference is that the type of the struct pointer can be cast. With these special usages and strict restrictions, the pointer to the class member function actually becomes useless. This is why we usually do not see "::*", ". *" and "->*" in the code.