Home>

C++ constant pointer to objectDeclare the pointer variable as const so that the pointer value always remains at its initial value,unable to be changed.

Such as:

 time t1 (10,12,15), t2;//define the object
  time * const ptr1;//const position before the pointer variable name,Specifies that the value of ptr1 is constant
  ptr1 =&t1;//ptr1 points to object t1, and can't change the direction after that
  ptr1 =&t2;//Error, ptr1 cannot change the direction

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

 Class name * const pointer variable name;

You can also initialize pointer variables when you define them,For example, merge lines 2 and 3 above:

 time * const ptr1 =&t1;//Specify ptr1 to t1

Note that the value of the constant pointer variable to the object cannot be changed,I.e. always point 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 constant pointers to objects?If i want to associate a pointer variable with an object (that is, the pointer variable always points to an object), you can specify it as a const pointer variable,This prevents misuse,Increase security.

Often use constant pointers as function parameters,The purpose is to not allow the value of the pointer variable to be changed during the execution of the function, Make it always point to the original object.If the value of this parameter is modified during function execution,The compilation system will find errors,Gives an error message,This is more reliable than manually ensuring that the parameter values ​​are not modified.

C++ pointer variable to constant object

To make it easier to understand the concept and use of pointer variables to constant objects,First understand the pointer variable to the constant variable,Then we will further study pointer variables to constant objects.The following defines a pointer variable ptr to a constant variable:

 const char * ptr;

Note that the position of const is on the far left,It is closely connected to the type name char, indicating that the char variable pointed to by the pointer variable ptr is a constant variable.It cannot be changed by ptr.

The general form of defining a pointer variable to a constant variable is:

 const type name * pointer variable name;

A few notes:

1) If a variable has been declared as a constant variable,You can only point to it with a pointer variable to a constant variable,You cannot use general pointers (pointers to non-const variables) to point to them.Such as:

 const char c []="boy";//Define const char array
  const char * pi;//Define pi as a pointer variable to a const char variable
  pi=c;//Legal, pi points to the constant variable (the first element of the char array)
  char * p2=c;//Illegal, p2 is not a pointer variable to a constant variable

2) In addition to pointer variables pointing to constant variables,You can also point to a variable that is not declared as const. The value of this variable cannot be changed by this pointer variable.Such as:

 char cl="a";//Define the character variable cl, which is not declared as const
  const char * p;//A pointer variable p that points to a constant variable is defined
  p =&cl;//make p point to the character variable cl
  * p="b";//Illegal, cannot change the value of the variable cl through p
  cl="b";//Legal, cl is not accessed through p, cl is not a constant variable

3) If the parameter of the function is a pointer to a non-const variable,The argument can only be a pointer to a non-const variable,Instead of using a pointer to a const variable,In this way, the value of the variable pointed to by the parameter pointer variable (that is, the variable pointed to by the actual parameter pointer) can be changed during the execution of the function.

If the parameter of the function is a pointer to a const variable,Obviously, the value of the variable pointed to by the pointer variable cannot be changed during the execution of the function.So allow 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);//The formal parameter of function fun is a pointer to a non-const variable
  fun (str);//The fun function is called, the argument is the address of the const variable,illegal

Because formal parameters are pointer variables to non-const variables,Theoretically, the value of the variable it points to can be changed during the execution of the function.But the parameter pointer and the argument pointer point to the same variable,The argument is the address of the const variable,The value of the variable it points to is immutable.This is a contradiction.Therefore, C++ requires the argument to use the address of a non-const variable (or a pointer variable to a non-const variable).

The corresponding relationship in the above table is consistent with the relationship between the pointer variables introduced in (2) and the variables they point to:pointer variables pointing to constant variables can point to const and non-const variablesPointer variables that point to non-const variables can only point to non-const variables.

The above is a pointer variable to a constant variable.The concept and use of pointer variables to constant objects is similar.Just replace "variable" with "object".

1) If an object has been declared as a constant object,You can only point to it with a pointer variable to a constant object,You cannot use general pointers (to non-const objects) to point to them.

2) If a pointer variable to a constant object is defined,And make it point to a non-const object, then the object it points to cannot be changed through a pointer.Such as:

 time t1 (10,12,15);//Define time class object t1, which is a non-const object
  const time * p =&t1;//define p as a pointer variable to a constant object,And point to t1
  t1.hour=18;//legal, t1 is not a constant variable
  (* p) .hour=18;//Illegal, don't hesitate to change the value of t1 through a pointer variable

If the value of t1 cannot be changed under any circumstances,It should be defined as const, such as:

 const time t1 (lo, 12,15);

Note the difference in form and function between a pointer variable pointing to a constant object and a constant pointer variable pointing to an object.

 time * const p;//constant pointer variable to object
  const time * p;//pointer variable to constant object

3) A pointer to a constant object is most often used as a parameter to a function,The purpose is to protect the object pointed to by the parameter pointer,Make it unmodified during function execution.

Remember this rule:when i want the value of an object to not be modified when the function is called,Formal parameters should be defined as pointer variables to constant objects,Also use the address of the object as an argument (the object can be const or non-const). If it is required that the object is not changed during the function call,And it is required not to change during the execution of the program,It should be defined as const.

4) If a pointer variable to a constant object is defined,It cannot be used to change the value of the object pointed to,But the value of the pointer variable itself can be changed.

c
  • Previous Introduction to special symbols commonly used in batch processing
  • Next Deep understanding of C # DateTime date formatting