C ++ const qualifier

Source: Internet
Author: User

C ++ const qualifier

Const qualifier:Use const to restrict objects so that the value of a const object cannot be changed once it is created.

 

When defining a const object, you must initialize it in either of the following ways:

 

 

const int x = 10;const int x = getValue();

The first is to assign a constant or constant expression to the object directly. during initialization in this way, the compiler replaces all objects in the compilation phase, for example: replace all the places where x appears in the program with 10.

The second method is to initialize x at runtime through the return value of the getValue () function.

 

By default, the const object is valid only in the file. To use the same const object in all files, you can add extern when defining the const object, use extern to declare the const object in other files, such:

 

Extern const int x = 10; // define and initialize a const int type object in main. cc. Extern const int x; // declare a const int type object in main. h.

Const reference:

 

When initializing a const reference, any expression can be used as the initial value, for example:

 

int x = 10;double y = 10.4;const int &r1 = x;const int &r2 = 10;const int &r3 = x * 10;const int &r4 = y;

In the last three cases, r is actually bound with a temporary amount, and the compiler will convert the code into the following:

 

 

int tmp = y;const int &r4 = tmp;

At this time, the value of r4 cannot be changed. When the value of y is changed, the value of r4 will not change, because r4 is a temporary reference.

 

 

The reference of const can reference a non-const object. The reference of const only limits the reference operation that can be involved, but does not limit whether the referenced object itself is a const object, for example:

 

int x = 10;const int &r = x;

Pointer and const:

 

Pointer to a constant:The value of the object to which the Pointer Points cannot be modified. The value of the pointer itself can be changed, that is, the pointer can be directed to another object again, for example:

 

Const int x = 10; int y = 20; const int * p1 = & x; const int * p2 = & y; // The pointer pointing to a constant can point to a constant * p2 = 10; // error. The value of the object pointed to by the pointer pointing to a constant cannot be changed, even if the object is very large

Const pointer:That is, a constant pointer, which must be initialized during definition and cannot be directed to another object. However, you can use this pointer to modify the value of the object to which the pointer is directed, for example:

 

 

Int x = 10; int * const p = & x; // you cannot modify the value of the pointer. You can only modify the value of const int * const p2 = & x; // a constant pointer to a constant object, that is, the value of the specified object cannot be modified or the value of the pointer itself cannot be modified.

Constexpr and constant expressions:

 

A constant expression is an expression that does not change the value and can be computed during compilation. The literal value is a constant expression. The const object initialized with a constant expression is also a constant expression. Whether an object (or expression) is a constant expression is determined by its data type and initial value. It is a constant expression only when the const type and initial value are constant expressions.

In a complex system, it is difficult to tell whether an initial value is a constant expression. C ++ 11 stipulates that, the variable can be declared as the constexpr type so that the compiler can verify whether the value of the variable is a constant expression. The variable declared as constexpr must be a constant and must be initialized using a constant expression, such:

 

Constexpr int mf = 20; // constant expression constexpr int limit = mf + 1; // mf + 1 is a constant expression constexpr int sz = size (); // only when size () is a constexpr function is a correct statement.

Generally, If you think a variable is a constant expression, declare it as the constexpr type.

 

 

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.