Class member pointer

Source: Internet
Author: User

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->.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.