C + + Const qualifier

Source: Internet
Author: User

Const qualifier: Use const to qualify the object so that the const object cannot be changed once it is created.

A const object must be initialized when it is defined, in two ways:

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

The first is to assign a constant or constant expression directly to an object, initialized in this way, and the compiler will replace all objects in the compilation phase, such as: Replace all occurrences of x in the program with 10.

The second approach is to initialize at run time, using the return value of the function GetValue () to initialize X.


By default, const objects are only valid within a file, and to use the same const object in all files, you can add extern when you define a const object, and use extern to declare the const object in another file, such as:

extern const int x = 10;//defines the const int type object in main.cc and initializes it. extern const int x;      //Declaration of const int type object in Main.h.

a const reference:

An arbitrary expression is allowed to be used as an initial value when initializing a const reference, such as:

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, the r is bound to a temporary amount, the compiler will change the code as follows:

int tmp = y;const int &R4 = tmp;

The value of R4 cannot be changed at this time, and the value of R4 will not change when Y is changed, because R4 is a temporary reference.


A const reference can reference a non-const object, and a const reference simply qualifies the reference to participate in the operation, and the referenced object itself is not a const object, such as:

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

pointers and Const:

pointer to constant: The value of the object pointed to by the pointer cannot be modified, the value of the pointer itself can be changed, that is, you can re-point to another object, such as:

const int x = 10;int y = 20;const int *p1 = &x;const int *p2 = &y;  A pointer to a constant can point to the very amount *p2 = ten;            Error, you cannot change the value of the object pointed to by a pointer to a constant, even if the object itself is a very good amount

Const pointer:A constant pointer, which must be initialized when defined, and no longer be able to point the pointer to another object, but the pointer can be used to modify the value of the object being pointed to, such as:

int x = 10;int * Const P = &x;     You cannot modify the value of the pointer itself, only the value that points to the object being referred to is const int * const P2 = &x; A constant pointer to a constant object that cannot modify the value of the object being referred to or modify the value of the pointer itself

constexpr and constant expressions:

A constant expression is an expression that does not change the value and can be evaluated during compilation. Literals are constant expressions, and const objects initialized with constant expressions are also constant expressions. Whether an object (or an expression) is a constant expression is determined by its data type and initial value, which is a constant expression only if the const type is initialized and the value is a constant expression.

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

constexpr int MF =;           A constant expression of 20 constexpr int limit = MF + 1;    MF + 1 is a constant expression constexpr int sz = size ();       only if size () is a constexpr function is the correct declaration statement

In general, if you assume that a variable is a constant expression, declare it as a constexpr type.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Const qualifier

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.