Pointer usage in C ++

Source: Internet
Author: User

C ++An important foundation of program design is classes and objects,Object PointerIs a very important part, including pointing to the objectPointerPointer to an object Member, this pointer, a constant pointer to an object, and a pointer to a common object.

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:

 
 
  1. Time t;  
  2. Time *p;  
  3. 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:

 
 
  1. Int * p;
  2. 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:

 
 
  1. Void (* p )();
  2. P = fun;
  3. (* 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:

 
 
  1. Void (Time: * p) (); // defines the pointer Variable p pointing to the Time Class Object member function
  2. P = & Time: gettime; // assign the gettime () Address of the Public member function of the Time class to the pointer Variable p
  3. (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.

 
 
  1. int box::vol()  
  2. {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

 
 
  1. int box::vol()  
  2. {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

 
 
  1. int box::vol()  
  2. {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:

 
 
  1. Time t1 (8, 8), t2;
  2. Time * const p = & t1; // constant pointer p points to object t1
  3. 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:

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

 
 
  1. Char c = 'A' // defines the character variable c, which is not named const
  2. Const char * p; // defines the pointer Variable p pointing to the constant variable
  3. P = & c; // p points to the character variable c
  4. * P = 'B'; // invalid
  5. 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.

The ing table of the actual parameters when using pointer variables as the form parameters:

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:

 
 
  1. Time t1 (8, 8), t2; // defines the object
  2. Const time * p = & t1; // defines the pointer p pointing to a common object and points it to t1
  3. 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 the record I learned about the C ++ pointer and a summary of the usage of the C ++ pointer. I hope it will be helpful for you to learn about C ++.

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.