Resolving pointers to objects in C + + using the _c language

Source: Internet
Author: User

C + + A constant pointer to an object
Declare the pointer variable to be a const type so that the pointer value remains its initial values and cannot be changed.

Such as:

  Time T1 (10,12,15), T2; Defines the object time
  * const PTR1;//const position in front of the pointer variable name, the PTR1 value is a constant
  ptr1=&t1;//ptr1 to the object T1, can not be changed to point to
  ptr1=& thereafter T2; Error, PTR1 cannot change point

The general form of defining a constant pointer to an object is:

  Class name * const pointer variable name;


You can also initialize the pointer variable when you define it, such as merging the 2nd, 3 lines above:

  Time * Const ptr1=&t1; Specifies that ptr1 points to T1

Note that the value of a constant pointer variable that points to an object cannot be changed, that is, always pointing to the same object, but you can change the value of the object it points to, such as T1.

When do I need to use a constant pointer to an object? If you want to permanently associate a pointer variable with an object (that is, the pointer variable always points to an object), you can designate it as a const pointer variable to prevent misoperation and increase security.

It is often used as a formal parameter of a function to not allow changing the value of a pointer variable to always point to the original object during function execution. If the value of the parameter is modified during the function execution, the compiler will find the error and give the error message, which is more reliable than the manual method to ensure that the parameter value is not modified.

C + + pointer variable pointing to a constant object
To make it easier to understand the concept and use of pointer variables that point to a constant object, first understand the pointer variable that points to the constant variable, and then further study the pointer variable that points to the constant object. A pointer variable ptr that points to a constant variable is defined below:

  const char *ptr;


Note that the const position is at the leftmost, and it is tightly attached to the type name char, indicating that the char variable that the pointer variable ptr points to is a constant variable and cannot be changed by PTR.

The general form of a pointer variable that defines a constant variable is:

  Const type name * pointer variable name;

A few notes:
1 If a variable has been declared as a constant, you can only point to it with a pointer variable that points to the constant variable, not with a normal pointer variable pointing to a non-const variable. Such as:

  const char c[] = "Boy"; Defines a const-type char array
  const char * PI;  The Pi
  =c  is defined as the pointer variable of the char variable pointing to the const type. Valid, Pi points to constant (first element of char array)
  char *p2=c//Illegal, p2 is not a pointer variable pointing to a constant variable

2 A pointer variable that points to a constant variable can point to a variable that is not declared as const, in addition to a constant variable. The value of the variable cannot be changed by this pointer variable at this time. Such as:

  char cl = ' a '; Defines the character variable cl, which is not declared as
  a const const char *p;//defines a pointer variable p = &cl;/That points the constant to the
  character variable cl
  *p = ' B '; Can not change the value of cl = ' B ' by P/
  /Legal, no access via P CL,CL is not a constant variable


3 if the formal parameter of a function is a pointer to a non-const variable, arguments can only be directed to a non-const variable, not to a pointer to a const variable, so that the value of the variable to which the parameter pointer variable is pointed (that is, the variable to which the argument pointer points) may be changed during the execution of the function.

If a function's formal parameter is a pointer to a const variable, it is obvious that the value of the variable pointed to by the pointer variable cannot be changed during the execution of the function, allowing the argument to be a pointer to a const variable or a pointer to a non-const variable. Such as:

  const char str[] = "Boy"; STR is a const array name
  void Fun (char * ptr);//function Fun parameter is a pointer to a non-const variable
  (str);//Call the fun function, the argument is the address of the const variable, illegal

Because a parameter is a pointer variable that points to a non-const variable, the value of the variable that it points to in the execution of the function can be changed. But the parameter pointer and the argument pointer point to the same variable, and the argument is the address of the const variable, and the value of the variable it points to is immutable. There is a contradiction. C + + requires that the argument use the address of a non-const variable (or a pointer variable that points to a non-const variable).

The corresponding relationship between the above table and the pointer variable described in (2) is consistent with the variable to which it is directed: A pointer variable pointing to a constant can point to a const and a non-const variable, whereas a pointer variable pointing to a non-const variable can only point to a non-const variable.

This is a pointer variable that points to a constant variable, and the concept and use of a pointer variable to a constant object is similar to this one, as long as you change "variable" to "object".

1 If an object has been declared as a constant object, you can only point to it with a pointer variable that points to a normal object, rather than pointing to it with a normal pointer variable (pointing to a non-const object).

2 If you define a pointer variable that points to a constant object and point it to an object that is not a const, the object it points to cannot be changed by the pointer. Such as:

  Time T1 (10,12,15); Defines the time class object T1, which is a non-const object
  const Time *P = &t1;//definition p is a pointer variable to a constant object and points to T1
  t1.hour = 18;//Legal, T1 is not a constant variable
  (* p). hour = 18; Illegal, not Tel齙齖小王孖 change the value of T1 by pointer variable

If you want the value of T1 to be unchanged under any circumstances, you should define it as a const type, such as:

  Const Time T1 (lo,12,15);

Notice the difference in form and function between the pointer variable to the constant object and the constant pointer variable pointing to the object.

  Time * const P; A constant pointer variable that points to an object
  const time *p;//pointer variable pointing to a literal object

3 The pointer to a regular object is most commonly used in a function's formal parameters, in order to keep it from being modified during function execution, to the object that the parameter pointer points to.

Keep in mind the rule that when you want the value of an object not to be modified when you call a function, you should define the parameter as a pointer variable to a constant object and use the address of the object as an argument (the object can be either a const or a non-const type). If the object is required to not be changed not only in the calling Function procedure, but also when it is not changed during program execution, it should be defined as a const type.

4 If you define a pointer variable that refers to a constant object, you cannot change the value of the object you are pointing to, but the value of the pointer variable itself can be changed.

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.