Const understanding-forum post by goodluckyxl (forgotten dog)

Source: Internet
Author: User

This article is mainly intended for C ++ learners who have misunderstandings about const syntax. I hope all those who are vague about this can read and find some answers.
.

The earliest idea of const was to replace the macro of Preprocessor # define to form the concept of constants. For constant const objects, const pointer and pointer to const, function const type parameter, const function return type, const class member, and const member function, and some summary of the last understanding of const to describe Const.
① Const object and const object
The two concepts are described as follows:
1. Int const object; // The number of const objects cannot be modified. Object = 2; Error
2. Const int object; // The content stored in the const int type cannot be modified.
For the two const types of 1 and 2 used for objects, the expressions are different but the effects are the same. Because the object itself stores content changes to the object, the object content changes, and the latter is also changing the former. So the semantics is the same.
② Const pointer and pointer pointing to const and their combination
The three concepts are described as follows:
1. int * const P; // the pointer P is const and cannot be modified, for example, p ++; // If you modify P, an error occurs.
// Modify the content pointed to by p * P = 2; // OK
2. Const int * P; // P is the content pointed to by the pointer pointing to an integer constant. P ++ cannot be changed; // OK
// * P = 2; // Error
3. Const int * const P; // the pointer P itself cannot be modified and all valid content of P cannot be modified.
// Modify * P = 2; error and P ++; Error
③ Const Parameter Modification and const modification of the return type
1. Const Parameter Modification
At this time, the specific usage of the function parameter modifier const ① The usage is the same in ②
For example, void fun (const int I) {I ++;} // error cannot modify the constant I
2. The usage of the const modifier function's return type is similar to that of ① ②. Only the modified object changes into a returned object.
Example: const int fun () {static int I; return I ;}
Int res = (fun () ++ // error cannot modify constants to return objects
④ Const class members and const member functions
1. Const Member
Const members of the class are allowed to be initialized during construction and cannot be changed later. We can know that the const member of the class is different from the common const variable. The const member of the class is meaningful for each object. Because it is initialized during the construction period, it is constructed only after the class is instantiated. Therefore, the const member of the class can be described as follows: the class is initialized during every instantiation of the class and cannot be changed during the lifecycle of the object.
2. Const member functions
Description: void class: memberfun () const {}; // all the class member variables modified by this const cannot be modified after this function is applied. This will bring some benefits in design and prevent unexpected handling of problems.
Summary:
<1> const constants generally enable the compiler to maintain a table without allocating space. When extern externally references this constant or "&" to get the address of this constant, the compiler will assign the address to it. The const mechanism is complicated.
<2> the const memory rule const modifies the last name. I was confused by the const modifier when I was a beginner. Later, I gradually summarized what I thought was the easiest to understand.
Example: const int I; in this case, only the int modified by const indicates that I is not a constant, but the content of I is a constant. Because C/C ++ expresses changes to I, that is, changes to I content, so I is similar to a const. You may wish to use the pointer const to try and understand it.
<3> all non-const types can be converted to the const type unconditionally, but the latter cannot be automatically converted to the former unless explicitly forced conversion removes the const type. This makes sense, because the const type is a special subset of non-const, and it is reasonable to convert from general to special, just like template specialization, the inherited upward ing is meaningful.
<4> remember that all contents modified by const cannot be changed forever. If forced conversion is performed manually, the compiler will not remind you. Because it is not obligated to do so, we should be careful when converting it.
<5> when dealing with const class member functions, we introduced mutable to modify the class member variables. The modified member variables can be modified in the const class member functions, the compiler is allowed. Other members not modified by mutable cannot be changed in the const member function according to the const rule.

Conclusion

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.