First lookCode:
Class student {public: Student (int I, string nm): schoolnumber (I), name (Nm) {} void getinfo () const {cout <"Name: "<name <Endl <" schoolnumber: "<schoolnumber <Endl;} void setinfo (string nm, int ID) {name = Nm; schoolnumber = ID ;} int schoolnumber; string name ;}; int main () {student S1 (001, "bird"); Student * pstudent = & S1; string Student: * ps_student = & Student:: Name; // define the class member pointer and initialize int Student: * pi_student = & Student: schoolnumber; void (Student: * pf_screen) () const = & Student :: getinfo; // define the class member function pointer and initialize void (Student: * pf_set_screen) (string, INT) = & Student: setinfo; // use the class member pointer cout <S1. * ps_student <Endl; cout <pstudent-> * pi_student <Endl; s1.getinfo (); (S1. * pf_screen )(); (pstudent-> * pf_screen) (); Return 0 ;}
Previously, we used pointers like this: initialize a pointer pointing to an int as the address of an int member of a specific object in a class:
Int * P = & s1.schoolnumber;
However, the idea has changed: we have a pointer that points to the int type members in a class and initializes the pointer to the address of a class member of an int type.
I want to use this pointer to access the class members of a specific object of this class (including member functions) through. * or->.