Review:
Definition of common function pointers1 Int(*PFI )()
Question:
Suppose there are the following classes: 1 Class Screen
2 {
3 Public :
4 Int Height (){ Return _ Height ;}
5 Int Width (){ Return _ Width ;}
6 // .
7 }
Now assign a value1 PFI= &Screen: height;
2 //Illegal value assignment, type violation
Because the pointer to a member function contains three aspects: 1) parameter table (number, type) 2) return type 3) class type
The normal pointer to a function contains only two aspects.
correct pointer definition to a member function: 1 int (screen :: * PMF) ();
2 PMF = & screen: height;
3
4
To avoid complex pointer types, use typedef1 TypedefInt(Screen*Ptr_type )();
2 Ptr_type PMF= &Screen: height;
Note:Classname ::*Syntax
Usage of pointers to class members: 1 Int (Screen :: * PMF )() = & Screen: height;
2 Screen myscreen, * Bufscreen;
3
4 // Directly call member functions
5 If (Myscreen. Height () = Bufscreen -> Height ())
6 //
7
8 // Equivalent call through the member function pointer
9 If (Myscreen. * PMF )() = (Bufscreen -> * PMF )())
10 //
In the same way, point to the data member pointer:1 IntScreen ::*Ps_screen= &_ Height
Conclusion: Compared with common pointersClassname ::*
Static member pointer:
We know that static class members belong to global objects and functions of the class, and their pointers are normal pointers (remember that static member functions do not use this pointer)
1 Class Classname
2 {
3 Public :
4 Static Void Rai ( Double Incr );
5 Static Double Interest (){};
6 //
7 Private :
8 Static Double _ Interestrate;
9 String _ Owner;
10 }
11
12 // OK
13 Double * PD = & Classname: _ interestrate;
14 // Error
15 Double Classname :: * PD = & Classname: _ interestrate;
16
17 // OK
18 Double ( * Pfunc )() = & Classname: interest;