Zhiyinjixu Summary: const usage

Source: Internet
Author: User

Const usage summary:

I. Const and pointer
Judgment Rules:
If the const is on the left side of the asterisk, the const is used to modify the variable pointed to by the pointer, that is, the variable referred to by the pointer cannot be modified;
If the const is on the right side of the asterisk, the const is to modify the pointer itself, that is, the const pointer.


Int A = 500;
Const int * P = & A; // First Class: pointer to const
Int const * P = & A; // no difference with the above
Int * const P = & A; // Second Class: const pointer
Const int * const P = & A; // Category 3: const pointer to const

First Class: pointer to const
(1) Symbolic Constants

The so-called "symbolic constant" is "Read-Only variable", that is, "const modified variable ". (Symbol constants are all variable modified by const)

Const int A = 5; // A is equivalent to a constant at this time, and the value of a cannot be changed.


(2) pointer to const:
The pointer to const can point to both common variables and symbolic constants. (Therefore, if you do not modify the value of a variable, it is best to define it as a pointer to the const, so that the pointer can point to either a common variable or a symbolic constant)
The following is a situation that points to a common variable:

① You cannot use this pointer to modify the value of a variable;
② The pointer can be modified;
③ The value of a variable can be modified;


Int A = 4, B = 6;
Const int * P; // defines the pointer to const
P = & A; // initialize the pointer
* P = 5; // error, description ①
P = & B; // correct, description ②
A = 88; // correct. ③


(3) pointer to a symbolic constant
A symbolic constant can only be indicated by a pointer to const, but cannot be a normal pointer:
Const int A = 0;
Int * P1 = & A; // Error
Const int * P2 = & A; // correct

Note: This method is often used in read-only arrays.
Int main ()
{
Const char a [] = "hello"; // defines a read-only Array
Const char * P = A; // it is incorrect to write char * P = A; because the variable modified with const can only be indicated by a pointer to const.
}

For read-only arrays, there is no pointing problem, but it is the same data type.


Second Class: const pointer

① The pointer itself is a constant and can only point to the data given at the time of definition, but cannot point elsewhere;
② The variable pointed to by the const pointer can be modified through the const pointer.

Int A = 2, B = 5;
Int * const P = &;
P = & B; // error, description ①
* P = 8; // correct, description ②

The const pointer can accept the const address and non-const address, but the non-const pointer can only accept the non-const address. Therefore, const pointers are more powerful, so it is a habit to use const pointers whenever possible.

Note: The const pointer mentioned in the above sentence can accept the const address. The const address refers to a symbolic constant. The const pointer cannot accept a symbolic constant, to accept a symbolic constant through the const pointer, you must use the const pointer pointing to the const, that is, the third category. He said this.

Category 3: const pointer to const
That is, the above two cases are combined

① You cannot use this pointer to modify the value of a variable;
② The const pointer cannot be modified.

Int A = 2, B = 5;
Const int * const P = &;
* P = 1; // error, description ①
P = & B; // error, description ②


Ii. Const and reference-regular reference

This is equivalent to the first class of const and pointer, that is, the pointer to const.

A constant reference can be either a reference of a common variable or a reference of a symbolic constant (so if you do not modify the value of a variable, you 'd better define it as a constant reference, this reference can be a reference to both common variables and symbolic constants .)
① Variable values cannot be modified through regular references
② You can modify the value of a variable through the variable itself.

Int A = 4;
Const Int & RA = A; // define a regular reference
Ra = 5; // error, description ①
A = 5; // correct, description ②

Note: regular references are common in <Operator overloading, such as: Friend ostream & operator <(ostream &, const &);

3. Other notes
(1) in C language, the length of the array cannot be a variable modified by const, but can be in C ++.

Const int A = 5;
Int M [a];
The above statement is correct in C ++, but it is not allowed in C. Think of constants in C language! = Read-Only variables. That is to say, constants cannot be defined using const in C. constants can be defined using the # define macro and enum type in 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.