Use of C + + const

Source: Internet
Author: User

Const is used to declare a constant, and when you do not want a value to be changed, use the const,
const INT max && int const Max is no different, all can.
Does not involve pointer const very well understood.


Cases involving pointers:
Conat int b=100, top-level const
int const c=11; top-level const

const INT *a=&b; Underlying const
[1] Const control (pointer *) *a cannot perform an assignment operation *a=3 (error) but can modify the stored address of a a=&c

int const *a=&b; Underlying const
[2] Const control (pointer *) *a cannot perform an assignment operation *a=3 (error) but can modify the stored address of a a=&c

int *const a=&b; Top-Level const
[3] Const control (variable) A, cannot be assigned operation a=3,a++ (error) but can modify the value of *a save B *a=10

const INT *const a =&b; Bottom and top-level const
[4] from right to left to see the const, the right of the const control A, the left of the const control *a, so here the 2 const can not change itself, can not change the saved address.

Const initialization
Features of the const:
Variables that are qualified with const cannot be changed.
Since the Const object definition cannot be changed, it must be initialized.
A const object's constant feature is only shown when trying to change it, and at other times it is the same as a variable.
Const initialization:
const int bufSize = 512; BufSize can no longer change
Const is only valid in this file
Const objects are usually only valid in this file, and if you want them to work in other files, you need to precede them with the extern keyword. A more detailed approach is to define a const in one file and declare and use it in several other files.
extern const int bufSize = 512;
Top-level const and underlying const
First, the const is a qualifier, and the value of the variable it modifies cannot be changed. For general variables, there is no difference between the top-level const and the underlying const, and only the basic variables of the composite type such as pointers have such a difference.
How do I differentiate between top-level const and underlying const?
The top-level const means that the pointer itself is a constant;
The underlying const indicates that the object pointed to by the pointer is a constant.
Pointers if you add a const modifier in two cases:
Pointer to constant: represents a pointer that cannot change its point to the content. When declaring a const can be placed before or after the type name, in the case of the int type, when declared: const int and int const are equivalent. To declare a pointer to a constant, which is the underlying const, give an example:
int num_a = 1;
int const *P_A = &num_a; Equivalent to the const int *p_a = &NUM_A, pointer to the const int type, is the underlying const
*p_a = 2; Error, pointer to "constant" cannot change the object being referred to
Note: A pointer to "constant" does not mean that the content it points to must be constant, but that the representation cannot be changed by the dereference (operator *).
In the above example, the pointer p_a to the content is not a constant, you can pass the assignment statement: num_a=2; To change what it points to.
Constant pointer: Represents the pointer itself as a constant, which must be initialized when declared, and the address value it stores can no longer be changed. A const must be placed behind a pointer symbol, that is, Const. Declaring a constant pointer is the top-level const, here's an example:
int num_b = 2;
int *const p_b = &num_b; A const pointer to type int, which is the top-level const
P_b = &num_a; Error, constant pointer cannot change stored address value
In fact, the top-level const and the underlying const is simple, a pointer itself adds a const qualifier is the top-level const, and the pointer refers to the object to add the const qualifier is the underlying const.
Distinguish between the role of the top-level const and the underlying const
Why do we have to differentiate between the top and bottom const, according to C++primer's interpretation, there are two roles to differentiate.
1 There is a limit to the execution of object copies, and the underlying const of a constant cannot be assigned to a very low-level const. In other words, you can avoid such assignment errors if you can correctly differentiate between the top and bottom Const. Here's an example:
int num_c = 3;
const int *p_c = &num_c; Pointer to const int, which is the underlying pointer
int *p_d = P_c; Error, cannot assign the underlying const pointer to a non-top-level const pointer
const int *p_d = P_c; Correct, all pointers to the const int
2 when using the named coercion type conversion function const_cast, you need to be able to distinguish between the underlying const and the top-level const, because const_cast can only change the underlying const of the operand. Here's an example:
int num_e = 4;
const int *p_e = &num_e;
*p_e = 5; Error, cannot change what the underlying const pointer is pointing to
int *p_f = Const_cast<int *> (p_e); Correctly, const_cast can change the underlying const of an operand. But be sure to know that num_e is not a const type when used.
*p_f = 5; Correct, non-top-level const pointers can change the point of the content
cout << num_e; Output 5
3 Practice
Speaking so much, should practice, const int constconst* Pppi is the top-level const or the underlying const?
The answer is of course the underlying const, because the int precedes the const qualifier, and the last * does not follow the const qualifier. Look at the last example:
const int a = 1; A is the top-level const
int * pi = &a; Error, &a is the underlying const and cannot be assigned to a non-underlying const
const int * pi = &a; Correctly, the &a is the underlying const, which can be assigned to the underlying const
const int *const *const PPI = &AMP;PI//is the underlying const and also the top-level const
const int *const *const *pppi = &ppi; Underlying const
Pointers and Const qualifiers (another version understands the method)
Pointer to const object
Const pointer
Const pointer to const object
Const double *p;//pointer to const double type
Double *const p = &pi; A const pointer to a double object
Const double *const p = &pi;//A const pointer to a const object
Here are a few examples:
Pointer to const object
#include <iostream>
using namespace Std;
int main ()
{
Double A = 1.2;
Double *p = &a; P is a pointer to variable a

Const double PI = 3.14;
p = &pi;//error, to point to a const object, you must use a pointer to the const object
Const double *CPTR = &pi; Const pointer to type double
*cptr = 1.5; Wrong, a pointer to a const object can only point to a const object and therefore cannot modify its value

System ("pause");
}
Const pointer
Const pointer to const object
#include <iostream>
using namespace Std;
int main ()
{
Double A = 1.2;
Double *p = &a; P is a pointer to variable a

Const double PI = 3.14;
p = &pi;//error, to point to a const object, you must use a pointer to the const object
Const double *CPTR = &pi;
Cptr = &a; A pointer to a const object can also point to a non-const object
*cptr = 1.5; But it cannot be modified by a pointer

int errnum = 0;
int *const Curerr = &errNum; A const pointer to an int object that must be initialized, and that the const pointer no longer points to another object

Const double *const pi_ptr = &pi; Const pointer to const double object
You can no longer point to other objects, and you cannot modify the value of an object by using the pointer

System ("pause");
}
Const write on the left also line, write on the right also line.
const string STR1;
string Const STR2;
Both of the above are the right way to write.

Use of C + + const

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.