// This program is used to differentiate the following six pointers: 1. Common pointer 2. Common function pointer 3. pointer to the data member of the class
// 4. pointer to the member function of the Class 5. pointer to the static data member of the class 6. pointer to the static member function of the class
# Include <iostream>
# Include <string>
Using namespace std;
Class Myclass
{
Public:
Myclass (int I = 0, int j = 0): _ weight (I), _ height (j ){}
Void print ()
{
Cout <_ weight <"," <_ height <"," <the_same <endl;
}
Static void modifythesame (int xx)
{
The_same = xx;
} // The static member function can only modify static data members.
Public:
Int _ weight, _ height;
Static the_same;
};
Int Myclass: the_same = 0;
Int max (int a, int B)
{Return a <B? B:;
}
Int a = 2, B = 3;
Myclass myclass;
Int * p1 = & a; // The first type: normal pointer
Int (* p2) (int, int) = max; // type 2: normal function pointer
Int Myclass: * p3 = & Myclass: _ height; // The third pointer: pointer to the data member of the class.
// The pointer must be added with the Class Name ::
Void (Myclass: * p4) () = & Myclass: print; // fourth pointer: pointer to the member function of the class, and
// The difference between the function pointer is that the class name is ::
Int * p5 = & Myclass: the_same; // The Fifth pointer: pointer to the static data member of the class. The left side is a normal pointer.
// Similarly, add the following class name to the right ::
Void (* p6) (int) = & Myclass: modifythesame; // type 6 pointer: pointer to the static member function of the class
// The left side is the same as the normal function pointer, and the right side should be added with the Class Name ::
Void main ()
{
Cout <* p1 <endl; // use of the first normal pointer
Cout <p2 (a, B) <endl; // use of the second normal function pointer
Cout <myclass. * p3 <endl; // use the pointer of the third data member pointing to the class to use the original data member name
// * Replace P.
(Myclass. * p4) (); // use the pointer to the class member function, replace the original member function name with * p, and
// Use () to enclose it. This place is easy to forget.
Cout <* p5 <endl; // use of the pointer to the static data member of the class, which is the same as that of the common pointer.
P6 (a); // the usage of the pointer to the static member function of the class, which is the same as that of the normal function pointer.
Cout <* p5 <endl;
}
/* The error is:
(1) No () is added when the fourth pointer is used ();
(2) _ weight was defined as private at that time, resulting in cout <myclass. * p3 <endl; no way to access private members
*/