Talking about the const Qualifier and the const limitation

Source: Internet
Author: User

Talking about the const Qualifier and the const limitation

What is a const qualifier?

A Const qualifier is a constant qualifier. Objects modified by const have a constant nature and can only be read but cannot be written.

 

Why use the const qualifier?

Replace "magic number" with the const variable to make the code easier to understand and maintain. For example, the const constant variable is used as the boundary of the array, and the const constant variable is used as the condition label of the switch.

The "minimum privilege principle" is embodied in C ++ to prevent data that should not be modified from being tampered. For example, many pointers (or iterators) move while reading rather than writing. Many function parameters are read-only and not written. Using the const qualifier can not only restrict parameter modification, you can also extend the range of parameters that the function can accept (const objects can be accepted as parameters ).

 

How to use it? Const qualifier

1. Variable: name of the const type variable, for example, const int ia = 5;

2. Reference: const type & reference name, for example, const int & ib = ia;

3. Object: const class name Object Name

4. pointer: const type * pointer name (constant pointer); Type * const pointer name (pointer constant)

5. array: const type array name [Arr_Max]

6. member functions: Class Name: function name (form parameter table) const

 

PS: in the above cases, except for pointer constants and const modifier member functions, const and type or class name (class is a custom type) can be exchanged, such as: const int ia = 5; equivalent to int const ia = 5. We recommend that you stick to one style.

 

Usage of const delimiters:

1. pointer

When using a pointer, two objects are involved: the pointer itself and the object pointed to by the pointer. Depending on whether the two objects are modified using const, the following four conditions are generated:

Int * ptr;

Constant pointer int * const ptr1 pointing to a very large object;

The constant object's constant pointer const int * ptr2;

The constant pointer to the constant object int const * const ptr3;

Explanation from right to left:

Ptr1 is a const pointer to int. ptr1 is a const pointer pointing to an int object.

Ptr2 is a pointer to const int. ptr2 is a pointer pointing to the const int object.

Ptr3 is a const pointer to const int. ptr3 is a const pointer pointing to the const int object.

 

If the pointer itself is modified as a constant, the pointer should be initialized during definition and cannot be changed later (that is, the pointer cannot be assigned a value ), however, you can modify the value pointing to the object by unreferencing it.

For example:

Int I = 10, j = 20;

Int * const ia = & I; // Initialization is required during definition

* Ia = 100; // correct. You can modify the value pointing to the variable.

Ia = & j; // error. It is not allowed to assign values to the Pointer Modified by const (even if the same value is assigned)

Ia = & I; // error. It is not allowed to assign values to the Pointer Modified by const (even if the same value is assigned)

 

If the object to which the Pointer Points is modified as a constant, the value of the object cannot be modified after the pointer is unreferenced, but the object to which the pointer can be changed.

For example:

Int I = 10, j = 20;

Const int * ia = & I;

* Ia = 100; // error. You cannot use a pointer to modify the value of the object.

Ia = & j; // correct. The pointer itself is not modified by const and can be changed to an object.

 

PS: it is not allowed to modify the value pointing to a variable through ia, but it does not mean that the value of the variable cannot be modified. We can still modify the value of the variable in other ways, for example, assign a value of I = 100 directly. Whether the value of a variable can be modified depends on the variable itself. The const modifies the variable pointed to by the pointer, which only indicates that the variable has the const attribute and cannot be modified by the pointer. It is also possible to use another pointer, for example, int * ib = & I; * ib = 100;

 

If the Pointer Points to the specific const feature of the object itself, the value directed to the object cannot be modified, including direct modification or indirect modification (via pointer or reference modification ). Therefore, the C ++ language requires that the pointer to the const object must also have the const feature. Otherwise, a compilation error occurs (C ++ primer Chinese version 4 ). This means that only a pointer to a constant object can point to a const variable.

For example:

Const int I = 10;

Int * ia = & I; // compilation Error

Const int * ib = & I; // correct

Int * const ic = & I; // compilation Error

Const int * const id = & I; // correct

2. Reference

A reference to a variable is the alias of a variable. The reference itself cannot be assigned another value to make a reference an alias of another variable. Therefore, const only needs to consider whether to modify the value of the bound variable by referencing the variable when modifying the referenced variable. There are only two types of references: reference of the const variable and reference of a non-const variable. For other features, the reference is consistent with the pointer.

For example:

Int I = 10;

Const int j = 20;

Const int & ra = I; // correct, indicating that the I value cannot be modified through ra

Int & rb = j; // compilation error. Only the reference of the const variable can be bound to the const object.

Const int & rc = j; // correct

Int & const rc = I; // syntax error. Unlike pointer, reference cannot be defined like this

 

PS: when a program is loaded, the system divides the memory into five areas: heap, stack, global (static), text constant, and program code. For the const modified variables, the system does not define a special region to protect the data in it from being modified. That is to say, the use of constant variables to protect data is achieved through syntax restrictions by the compiler. We can still modify the region restricted by const to "constant" by bypassing compiler restrictions. For example, in C speech, the value of the const variable is indirectly modified through the pointer, and in C ++, the value of the const variable is modified using the volatile keyword. However, in the C ++ standard, the actual result of modifying the const variable is "undefined behavior" depends on the implementation of various compilers. Therefore, it is not recommended to modify the value of the const variable. (This is not a nonsense. If you want to modify it, what is defined as const ).

 

...... To be continued

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.