In C ++, pointers are used a lot, and many pointer usage can be confused, after I borrowed a copy of "C ++ object-oriented programming" Edited by Professor Tan haoqiang from the library, I helped me sort out my ideas. I want to help you with some of the content.
Classes and objects are an important basis for C ++ program design, and object pointers are an important part, including pointer to object, pointer to object Member, this pointer, constant pointer to object, pointer to constant object, etc.
1. pointer to object
Definition: the starting address of the object space is the object pointer.
Note: When creating an object, the compilation system allocates a certain amount of storage space for each object to store its members, in general, the data stored in data storage units of different objects is different, while the function code of different objects is the same, that is, their function code is shared. In this case, we can define a pointer variable to store the object pointer.
The general form of defining pointer variables pointing to class objects is:
Class name * Object Pointer name;
For example, for a Time-class object, we can:
Time t;
Time * p;
P = & t;
We can use object pointers to access objects and object members. If the defined class contains data members hour, minute, and sec, and the member functions include gettime (),
(* P). hour refers to the hour member in the object pointing to p, which is equivalent to t. hour.
(* P). gettime () indicates that p points to the member function gettime () in the object, which is equivalent to t. gettime ()
You can also use the following format:
P-> hour and p-> gettime () are equivalent to the above.
2. pointer to an object Member
(1) pointer to an object data member
In C, we have learned the pointer variable pointing to a common variable. In C ++, the method for defining the pointer variable pointing to an object data member is the same as the method for defining the pointer variable pointing to a common variable, the general form is:
Data Type name * pointer variable name;
For example:
Int * p;
P = & t. hour; // assign the address of the data member hour of object t to p, and p points to t. hour.
(2) pointer to the object member function
The pointer variables defined to object member functions are different from the pointer variables defined to common functions.
When defining pointer variables pointing to common functions, we can define them as follows:
Void (* p )();
P = fun;
(* P) (); // call the fun Function
However, the compilation system requires that three conditions must be met when the function address is assigned to the pointer variable:
Function parameter types and numbers must match
The type of the function return value must match
The class to be matched
Obviously, the above p is irrelevant to the class. To meet the third requirement, we can specify a class for the pointer. Therefore, the pointer variable defined for the object member function is generally in the form:
Data Type (Class Name: * pointer variable name) (parameter table column );
Allows pointers to common member functions, such:
Void (Time: * p) (); // defines the pointer Variable p pointing to the Time Class Object member function
P = & Time: gettime; // assign the gettime () Address of the Public member function of the Time class to the pointer Variable p
(T. * p) (); // call the Time Class Object t's member function gettime ()
Note: because member functions are not stored in the object space and multiple similar objects share the code of this member function, the entry address of the member function assigned to the pointer variable should be written:
Pointer variable pointing to the object member function = & Class Name: member function name;
There is no "()" after the member function name. If it is written as p = & Time: gettime (), it is incorrect. [Nextp
3. this pointer
Each member function contains a special pointer called this, which is a pointer to an object of this class. Its value is the starting address of the object where the currently called member function is located. This pointer is automatically implemented by the system to ensure that the member functions of different objects of the same type reference data members in the specified object.
For example, define a function to calculate the volume.
Int box: vol ()
{Return (height * width * length );}
If the object t has been defined, when the member function t is called. when vol () is used, the compilation system assigns the starting address of object t to this pointer, so when a member function references a data member, based on the pointer this, You can reference the data member of object t. Therefore, C ++ processes the above functions
Int box: vol ()
{Return (this-> height * this-> width * this-> length );}
Because the value of this is the starting address of the object where the member function is currently called, it can be written
Int box: vol ()
{Return (* this). height * (* this). width * (* this). length );}
Therefore, the member function t is called. the actual call Method for vlo () is t. vol (& t), but the address of object t passed to this pointer is automatically completed by the system and does not need to be manually added.
4. A constant pointer to an object
Declare the pointer variable pointing to the object as the const type and start the process. In this way, the pointer value is always the initial value and cannot be changed.
For example:
Time t1 (8, 8), t2;
Time * const p = & t1; // constant pointer p points to object t1
P = & t2; // attempts to change the point of p, which is invalid.
From the above we can see that the general form of defining a constant pointer to an object is:
Class name * const pointer variable name = start address of the object;
Note: the value of the constant pointer variable pointing to the object cannot be changed, but the value of the data member pointing to the object can be changed.
A regular pointer is generally used as a function parameter, so that it is not allowed to change the value of the pointer variable during function execution so that it always points to the original object.
5. pointer variables pointing to common objects
First, let's review the pointer variable pointing to a constant variable. Its general form is:
Const type name * pointer variable name;
For example:
Const char * p;
If a variable has been declared as a constant variable, it can only be pointed to by a pointer variable pointing to a constant variable, rather than a general pointer variable. In addition to a constant variable, a pointer to a constant variable can also point to a variable that is not declared as a const, but cannot change its value through this pointer. For example:
Char c = 'A' // defines the character variable c, which is not named const
Const char * p; // defines the pointer Variable p pointing to the constant variable
P = & c; // p points to the character variable c
* P = 'B'; // invalid
C = 'B'; // valid
Note: In the above example, the pointer Variable p points to the character variable c, not to declare c as a constant variable, but to reference c through the pointer variable, c is a constant variable and its value cannot be changed, but c is still a common variable.
In pointer pointing to a constant variable, there are several points worth noting about the pointer type of a function parameter:
If the function parameter is a non-const pointer variable, the real parameter can only be a pointer to a non-const type. If the function parameter is a pointer to a const type variable, real parameters can be const-type or non-const-type pointer variables. In other words, a pointer to a constant variable can point to a const and a non-const type variable, while a pointer to a non-const type variable can only point to a non-const type variable.
Table of mappings between the form parameter and the real parameter when the pointer variable is used as the form parameter
Parameters |
Real Parameters |
Legal or not |
Change the value of the object indicated by the pointer |
Pointer to a non-const variable |
Addresses of non-const Variables |
Valid |
Line |
Pointer to a non-const variable |
Address of the const variable |
Invalid |
/ |
Pointer to a const variable |
Address of the const variable |
Valid |
No |
Pointer to a const variable |
Addresses of non-const Variables |
Valid |
No |
The pointer variable pointing to a constant object is similar to that pointing to a constant variable pointer variable.
If an object has been declared as a long object, it can only be pointed to by a pointer variable pointing to a common object, rather than a general pointer variable pointing to it.
If you define a pointer variable that points to a common object and points it to a non-const object, the object to which it points cannot be changed through the pointer.
If a pointer variable pointing to a common object is defined, it cannot be used to change the value of the object to which it points, but the value of the pointer variable itself can be changed.
For example:
Time t1 (8, 8), t2; // defines the object
Const time * p = & t1; // defines the pointer p pointing to a common object and points it to t1
P = & t2; // valid, pointer p points to object t2
So when we want to call a function without modifying the object value, we can define the form parameter as a const-type pointer variable, at the same time, the object address is used as the real parameter (the object can be of the const or non-const type). When the object is required not to be changed not only in the called function, but also during the program execution, we define it as the const type. Therefore, pointers to common objects are most often used in function parameters to protect the objects pointed to by the parameter from being modified during function execution.
The above is a record of how I learned the C ++ pointer and a summary of the usage of the C ++ pointer. I think it is helpful for everyone to learn C ++.