Const use

zhaozj2021-02-16  73

Const modified pointer and reference usage, people who begin school C are straightforward, I don't know how to be cloud. Once you know the usage, everything is worth a good.

It can be divided into three situations: const modified pointers, const modified references, const modified pointers.

1.const modified pointer

The Const modifier pointer can be divided into three situations:

Const modified pointer itself

CONST modified pointer to variables (or objects)

Const modified pointer itself and pointers referred to (or object)

(1) .const modified pointer itself

In this case, the pointer itself is constant, which cannot be changed, and any modified pointer itself is illegal. For example:

Const int a = 1;

Const Int B = 2;

INT i = 3;

INT J = 4;

INT * const pi = & i; // ok, the type of INT * const, & i is int * const

INT * const pi = & a; // error, PI type is int * const, & a type of CONST INT * ConST

Pi = & J; // Error, pointer is constant, not variable

* Pi = a; // ok, * pi is not limited to constant, variable

As a result, PI is constant, and the constant is in the initialization and assignment, the type must be strict. That is

When the const modifier pointer itself, the variable type on both sides must be strict, otherwise it cannot be matched.

(2). Const modified pointer points to variables (or objects)

In this case, the value of the variable cannot be changed by indirect reference pointer, assuming that the pointer is P, then * p is unambiguous, and below will be described below:

Const Int * Pi = & A;

// or int const * pi = & a;

// The two are dead, but BS likes the former, this is not technically the advantages and disadvantages, that is, Const Int and int Const can be interchangeable. It is recommended to be familiar

// Take some of these two forms, for simple, and later use the former.

// OK, const does not modify the pointer itself, PI pair assignment type

// There is no request, but the PI is an int * type pointer, so the assigned must be an address value.

Const int * pi = & i; // ok, PI can assign the address of the constant, but also to empower the address

Const Int * Pi1 = & A;

Const Int * Pi = Pi1; // OK

* Pi = j; // error, * pi is not variable, can not change the indirect reference form of the pointer

Pi = & j; // ok, Pi variable

Pi = & b; // ok, pi variable

Pi ; // ok

--PI; // ok

It can be seen that the PI is a variable, which can assate a value of a variable, as a integer variable can be assigned to the integer and integer variable. Constly modified is not the pointer itself, but an indirect reference, = number of two sides Don't strictly match, such as: const INT * PI = & A; medium, PI type is int *, and & A type is const Int * const, as long as it contains INT *. Another example: const INT * PI = & J; medium, the type of PI is int *, and the type of & j is int * const, it is not a case to PI.

(3) Const modified pointer itself and the variables referred to in the pointer (or object)

With pointer P, in this case, P and * P are not variable. For example:

Const Int * const pi = & a;

// or int const * const pi = & a;

// While it is the same as (2) as described in (2), it is only as described above, as mentioned above, the type on both sides does not have to match, but must contain int *, & a type of CONST INT * const, contains INT *, so you can assign a value. Const int * const pi = & i; // ok, & i type INT * const, INT *, can be assigned.

Const Int * Pi1 = & J;

Const Int * const pi = pi1; // ok, PI1 type is int *

Pi = & b; // error, pi is not variable

Pi = & j; // error, PI is not variable

* pi = b; // error, * pi is not variable

* pi = j; // error, * pi is not variable

PI ; // error, pi is not variable

i; // ok, = = Number of variables (or objects) independent of the modified variables

A-; // error, A is const

This situation is associated with the above two situations. For Const Int * const pi = & a; we can see: const INT * (const pi) = & a; (only expressions need), regard Const Pi as an integration, in line with the above classification (2). As long as INT * is contained.

2. Const modification reference

This situation is relatively simple, and there is no complicated as a modifier pointer, because reference and reference objects are integrated, there is only one type of const modification.

Const modification reference, reference itself is not variable, but referenced variables (or objects) can be changed. For example:

Const int & ri = a; // or int const & ri = a; OK, Ri itself is constant, reference is not distinguished

Const int & ri = i; // ok, reference is not distinguished

Ri ; // error, Ri is constant, not variable

i ; // ok, = = nothing to do with the reference

Ri = b; // error, ri is constant

i = j; // ok, = The variable on the right is independent of the reference

INT & const ri = i; // error, there is no such form, no meaning

3.Const modified pointer reference

Quote is just an individual name, here is similar to the modified pointer, and divided into three situations:

(1)

First give an example:

Const Int * Pi = & A;

Const Int * & ri = Pi;

// or int const * & ri = pi;

The reference is an alias that references the object, because of this, Ri is the alias of PI, so the type of Ri must be completely consistent with PI. The type of PI here is int *, and the type of Ri is also int *, assignment is feasible. If const Int * & ri = & a; is it correct? Analysis is aware of it. The RI type is int *, and the type of & a does not match the Const Int * Const.

const Int * & ri = & I; // Error, type does not match, one is int *, one is int * const

Ri = & a; // ok

Ri = & i; // ok

Const Int * Pi1 = & A;

Const Int * Pi2 = & I;

Ri = pi1; // ok

Ri = pi2; // ok

* ri = i; // error

* ri = a; // error

Note that this is different from 1- (2).

(2) Use example:

INT * Const & Ri = & I;

Remove the RI left on the left of the RI, it is int * const ri, because ri is an alias, so the type of Ri should match the number of assignments, the RI type is int * const, & i is Int * const, can do this.

INT * const & ri = pi; // error, type is not combined, one is int * const, one is int *

INT * const & ri = & a; // error, type is not combined, one is int * const, one is const INT * Const

(* ri) ; // ok

i ; // ok

Ri = & i; // error

In this case, Ri is constant and cannot be changed.

(3)

Example description:

Const Int * Pi = & J;

Const Int * const & ri = pi; // or int const * const & ri = pi; ok

Const Int * const & ri = & i; // ok

Ri is the alias of PI, and the type of PI should be consistent with RI. Take the CONST INT * Const Ri, regarding const ri as one, and it is easy to obtain the type information of Ri, just as discussed in front of 2- (3), as long as the Type INT is included. *. The type of PI is int *, & i is Int * const, which can be done.

Const Int * const & ri = & a; // ok

Ri ; // error

* ri = 6; // error

转载请注明原文地址:https://www.9cbs.com/read-16997.html

New Post(0)