Object Pointer in C ++

Source: Internet
Author: User

When a pointer to an object is created, the mutation system allocates a certain amount of storage space for each object to store its members. The starting address of the object space is the object pointer. You can define a pointer variable to store object pointers. Example 1.1: [cpp] # include <iostream> using namespace std; class Student {public: int num; int score; Student (int, int ); // declare the constructor void Print (); // declare the output information function}; Student: Student (int n, int s) {num = n; score = s ;} void Student: Print () {cout <num <"\ t" <score <endl;} int main () {Student stud (1,100 ); // instantiate an object Student * p = & stud; // define a pointer to the object and use the address of the object stud to initialize cout <"* p. num: "<(* p ). num <endl; // call the data member's Method 1 cout <"p-> score:" <p-> score <endl; // method 2 (* p) for calling data members using pointers to objects ). print (); p-> Print (); return 0;} the pointer object pointing to the object member has an address. The pointer variable that stores the initial address of the object is the pointer variable pointing to the object. The members in the object also have an address. The pointer variable that stores the address of the object member is the pointer variable pointing to the object member. 1. the pointer to the object data member defines the method to point to the object data member and the method to point to the common variable is the same: Data Type * pointer variable name [cpp] p = & t1.hour; // assign the address of the data member hour of object t1 to p, and p points to t1.hour cout <* p <endl; // output the value of t1.hour 2. the pointer definitions pointing to object member functions are different from the pointer definitions pointing to member functions. The method for defining pointers to common functions is as follows: Data Type name (* pointer variable name) (parameter list); for example: [cpp void (* p) (int n, int m); // p is the pointer variable pointing to the void FUNCTION p = fun; // assign the entry address of the fun function to the pointer Variable p, p points to the function fun (* p) (a, B); // calls the fun function, where,, B is a real parameter, and it is more complicated to define a pointer variable pointing to an object member function. If we define pointer variables directly according to the above method, assign the object member function name to the pointer Variable p: [cpp] void (* p) (); p = t1.Print (); an error occurs during compilation. A member function is the most fundamental difference from a common function: it is a member of the class. The compilation system requires that in the preceding copy statement, the type of the pointer variable must match the type of the function on the right of the value assignment number. The following three fields must be matched: 1. function parameter type and number of parameters 2. type of the function return value 3. the first two of the classes match, but the third point does not. The pointer Variable p has nothing to do with the class, while Print () belongs to the Student class. To define a pointer variable pointing to a member function, use the following form: [cpp] void (Student: * p )(); // define p as the data type name of the pointer variable pointing to the Public member function in the Student class (Class Name: * pointer variable name) (parameter table column); we know, the objects instantiated by all Student classes share a member function, so we do not need to specify that it is a pointer to the member function of the object, we only need to define it as a member function pointing to the class. The common form of pointing pointer variables to a common member function is: [cpp] p = & Student: Print; pointer variable name = & Class Name: member function name; in use, you still need to put the pointer in a specific object for use. (* p) is equivalent to Print () [cpp] stud. (* p); // function and stud. print () equivalent =============================== an example of using object pointers 1.2 ================= [cpp] # include <iostream> using namespace std; class Student {public: int num; int score; Student (int, int); void Print () ;}; Student: Student (int n, int s) {num = n; score = s;} void Student: Print () {cout <num <"\ t "<Score <endl;} int main () {Student stud (1,100); cout <" stud. print () "<endl; stud. print (); // directly call the member function Print () of the object stud; Student * p_class = & stud; // defines the pointer to the object stud; cout <"p_class-> Print ()" <endl; p_class-> Print (); // call the member function Print () by pointing to the object pointer; void (Student:: * p_fun) (); // declare that p_fun is the pointer to the function in Student p_fun = & Student: Print; // It is p_fun pointing to the Public member function Print () of the Student class () cout <"(stud. * p_fun) () "<endl; (stud. * p_fun) (); // call p _ In the stud object _ The member function (stud. print) return 0;} this pointer data members in each object occupy storage space. If n objects are defined for the same class, then there are n groups of data members in the same size as 1. Objects with different Singular Values call the same function code segment. So how can we ensure that the data member of the specified object is referenced when the member functions of different objects reference data members? In fact, every member function contains a special pointer. the pointer name is fixed and is called this pointer. It is a pointer to an object of this class, and its value is the starting address of the object where the currently called member function is located. For example, in the Print () function, [cpp] cout <num <"\ t" <score <endl; it is actually [cpp] view plaincopycout <this-> num <"\ t" <this-> score <endl; this indicates that the pointer is implicitly used, it is passed to the member function as a parameter. Function in the program: [cpp] void Student: Print () {cout <num <"\ t" <score <endl ;} the C ++ compilation system will automatically process it as: [cpp] void Student: Print (Student * this) {cout <this-> num <"\ t" <this-> score <endl;} adds the this pointer to the table column of the member function. [Cpp] stud. Print (& stud );

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.