Object pointers in C + + summary _c language

Source: Internet
Author: User

Pointer to Object
When creating objects, the mutation system assigns a certain amount of storage space to each object to store its members.
The starting address of the object space is the pointer to the object. You can define a pointer variable that holds a pointer to an object.

A simple example of 1.1:

Copy Code code as follows:

#include <iostream>
using namespace Std;
Class Student
{
Public
int num;
int score;
Student (int, int);/Declaration Constructor
void Print ()//Declaration Output Information function
};
student::student (int n,int s)
{
Num=n;
Score=s;
}
void Student::P rint () {
cout<<num<< "T" <<score<<endl;
}
int main () {
Student Stud (1,100);//Instantiate an object
Student *p=&stud;//defines a pointer to an object and initializes it with the address of the object stud.
cout<< "*p.num:" << (*p). num<<endl;//use a pointer to an object to call a data member
cout<< "P->score:" <<p->score<<endl;//use a pointer to an object to call a data member two
(*p). Print ();
P->print ();
return 0;
}



Pointer to object member
Object has an address, and a pointer variable that holds the initial address of the object is a pointer variable that points to the object.
The member of the object has an address, and a pointer variable that holds the member address of the object is a pointer variable that points to the member of the object.

1. Pointer to object data member
You define methods that point to object data members and define the same methods that point to common variables:
Data type * pointer variable name

Copy Code code as follows:

p=&t1.hour;//assigns the address of the data member hour of the object T1 to P,p to point to T1.hour
Value of cout<<*p<<endl;//output T1.hour

2. Pointer to object member function
There are different ways to define pointers to member functions and to define pointers to ordinary functions.

The way to define pointers to normal functions is this:
Data type name (* pointer variable name) (parameter list);
Such as:

Copy Code code as follows:

void (*p) (int n,int m);//p is a pointer variable that points to a void function
p=fun;//assigns the entry address of the fun function to the pointer variable p,p points to the function fun
(*p) (a,b);//Call fun function, where a,b is the argument

It is more complex to define a pointer variable that points to an object member function.
If we define the pointer variable directly according to the above method, assign the object member function name to the pointer variable p:
Copy Code code as follows:

void (*p) ();
P=t1. Print ();

The compilation will cause an error
One of the most fundamental differences between a member function and a normal function: It is a member of a class.
Compiling system requirements in the above copy statement, the type of the pointer variable must match the type of the right-hand function of the assignment number, which requires matching in 3 ways:
1. The type and number of parameters of the function parameter
2. Type of function return value
3. The class to which it belongs
The first two are matched, and the 3rd does not match.
The pointer variable p is independent of the class, whereas print () belongs to the student class.

To define a pointer variable that points to a member function, the following form should be used:

Copy Code code as follows:

void (Student::* p) ();//defines p as a pointer variable that points to a public member function in the Student class

Data type name (class name::* pointer variable name) (Parameter table column);

We know that all student classes instantiate objects that are common to a member function, so we don't have to specify that it is a pointer to the member function of that object, we just need to define it as a member function that points to that class.

The general form of the pointer variable pointing to a common member function is:

Copy Code code as follows:

P=&student::P rint;

Pointer variable name =& class name:: Name of member function;

When used, it is still necessary to place the pointer in a specific object, which (*p) is equivalent to print ()

Copy Code code as follows:

Stud. (*p);/action and stud. The role of Print () is equivalent

================= An example of using object pointers 1.2===========
Copy Code code as follows:

#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::P rint () {
cout<<num<< "T" <<score<<endl;
}
int main () {
Student Stud (1,100);
cout<< "Stud. Print () "<<endl;
Stud. print ();//directly invokes the member function print () of the object stud;
Student *p_class=&stud;//defines pointers to object stud;
cout<< "P_class->print ()" <<endl;
P_class->print ();//Call member function Print () by pointing to the object;
void (Student::* p_fun) ()//Declaration P_fun is a pointer to a function in Student
P_fun=&student::P rint;//is P_fun point to Student class public member function Print ()
cout<< "(Stud.*p_fun) ()" <<endl;
(Stud.*p_fun) ()//the member function (that is, stud) pointed to by the p_fun in the calling object stud. Print)
return 0;
}

This pointer
The data members in each object occupy storage space, and if n objects are defined for the same class, then there are n groups of the same size space that holds the data members of N objects.
Each singular object invokes the same section of the function code.
So, when the member functions of different objects refer to the data members, how can you guarantee that the data members of the object are referenced?
In fact, in each member function, a special pointer is included, and the name of the pointer is fixed, called the this pointer.
It is a pointer to an object of this class whose value is the starting address of the object where the currently invoked member function is located.

For example:
In the Print () function

Copy Code code as follows:

cout<<num<< "T" <<score<<endl;

is actually
Copy Code code as follows:

cout<<this->num<< "T" <<this->score<<endl;

This pointer is implicitly used and is passed as a parameter to a member function.
Functions in a program:
Copy Code code as follows:

void Student::P rint () {
cout<<num<< "T" <<score<<endl;
}

The C + + compilation system is automatically processed as follows:
Copy Code code as follows:

void Student::P rint (Student *this) {
cout<<this->num<< "T" <<this->score<<endl;
}

That is, add a this pointer to the parameter list column of the member function. In the call member function Stud. Print () is actually invoked in the following ways:
Copy Code code as follows:

Stud. Print (&stud);

Related Article

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.