C + + Learning Note <const qualifier >

Source: Internet
Author: User

1, cons can limit a variable, yes its value cannot be changed

2. By default, const objects are only valid within a file.

At compile time, the compiler replaces all occurrences of the const variable in the program with the initial value of const, so that every file used in a const variable is defined with a const variable, in order to support this usage, and to avoid multiple definitions of the same variable. The const object is set to be valid only within a file, and when a const object of the same name appears in more than one file, it is equivalent to defining separate variables in each file separately. If you want to use the same const variable in a different file, both the const declaration and the definition must precede the const with the extern keyword, so that you can define multiple uses at once.

3, const and reference:

You can bind a reference to a const object as if it were bound to another object, and we call it a reference to a constant, unlike a normal reference, a reference to a constant cannot modify the object it is bound to.

###//Excerpt from C++primer

C + + programmers often refer to "Const reference" as "constant reference", this abbreviation is very reliable, but the premise is that you have to remember this is a short name only.

Strictly speaking, there is no constant reference. Because references are not objects, we cannot let the reference itself be constant. In fact, since the C + + language does not allow arbitrary changes to the objects bound by the reference, it is a constant to understand all the references in this sense. The referenced object City constant is still a very good amount of action that can determine what it can participate in, but it does not affect the binding relationship of the reference and the object in any way.

##########################

A generic reference must have a type that is consistent with the type of object it refers to, and can only be bound to an object and cannot be bound to the results of an expression, with two exceptions. ① allows arbitrary expressions to be used as initial values when initializing a constant reference, as long as the result of the expression can be converted to a reference type. In particular, a constant reference to a const object, literal value, or even a generic expression is allowed:

Example: Double dval=3.14; const INT &ri=dval;

The compiler converts code at compile time into the following form: const int temp=dval;    const INT &ri=temp; A temporary amount was generated, and the const reference was bound to this temporary amount.

Instead of a const reference, this can not be used.

4. A reference to a const may refer to an object that is not const, and a constant reference restricts only the actions that the reference can participate in, and whether the referenced object itself is not a constant.

5. Pointers and const

As with references, you can also point a pointer to a constant or a very good amount. Like a constant reference, a pointer to a constant cannot be used to change the value of the object it refers to. To store the address of a constant object, you can only use pointers to constants.

const int a=1;

int * b=&a; Error.

As with constant references, pointers to constants do not stipulate that the object being referred to must be a constant. A pointer to a constant only requires that the value of the object cannot be changed by the pointer.

Unlike pointers and references, the pointer itself is an object, so you can position the pointer itself as a constant. A constant pointer must be initialized, and once initialized, its value (the address of the store) cannot be changed. The pointer is a constant before placing the * on a const. This form of writing implies that the constant is the value of the pointer itself, not the value pointed to by the pointer.

The pointer itself is a constant and does not mean that the object it points to cannot be modified by the pointer.

C + + Learning Note <const qualifier >

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.