Description:
This is "Notes" when I saw "Thinking In C " today, and some people asked about the question of the members' function pointer. I said that I have passed directly into the pointer. Now I have seen it. Method, however, its syntax is a bit difficult. This goes back to see how MFC's message mapping / dynamic creation is written, I think it should be a member function pointer
The pointer is a variable pointing to some memory addresses, or either the address of the data or the address of the function. C member pointer follows the same principles. Difficult is that all pointers need an address, but there is no address inside the class; the member of choosing a class means offset in the class. Only the actual address can only be obtained by combining this offset and the start address of the specific object. The syntax of the member pointer requires the selection of an object to the reverse reference member pointer.
Struct Simple {Int A;} Simple So; Simple * sp = & SO;
If there is a pointer SP and object SO, if a pointer is a member of a class object, even if it means that it represents a certain offset within the object, what will happen? In order to obtain the content pointing to the pointer, it must be retrograde with *. However, it is just an offset within an object, so you must also specify that object. Therefore, the * is binding to the object and the reverse reference.
SP -> * PM = 47; SO. * PM = 47;
What is the syntax of the PM? In fact, it is like any pointer, you must say what type it does. Also, use a '*' number in the definition. The only difference is just to say what kind of object used this member pointer. Of course, this is achieved by class name and global operator:
Define member pointer: int Simple :: * Pm;
Definition and initialize the member pointer: int Simple: * PM = & Simple :: A; Because reference to a class rather than the object, & Simple: A can only be represented as a syntax of the member pointer.
Pointer to the function is defined in the form below: int (* fp) (FLOAT); (* fp) cracker is used to force the compiler to correctly determine the definition. Without parentheses, this expression is a function that returns an int * value. In order to define and use a pointer to a member function, parentheses play an equally important role. Suppose there is a function in a structure:
Struct Simple2 {INT F (FLOAT);
You can define a pointer to a member function by inserting a class name and a global operator: int (simple2 :: * fp) (float);
Initialization: int (simple2 :: * fp) (float) = & simple2 :: f; & number is optional; you can use a function identifier without a parameter table: fp = simple2 :: f;
Use: SIMPLE2 S2; INT I = (S2. * Fp) (1.5);
Another example example CB {INT F1 () {RETURN 1;} int F2 () {return 2;} int F2 () {return 2;} int (cb :: * fptr [2]) (); public: cb () {FPTR [0] = CB :: F1; FPTR [1] = & CB :: F2;} int SEL (INT i) {return (this -> * fptr [i]) ();}};