Advance C + +--Const

Source: Internet
Author: User

This blog is based on Bo Qian's YouTube tutorial, which you can find on YouTube and a summary.

This section summarizes the understanding of the const:

Learn C + + know that the const in C + + is used to modify constants, it means that the immutable, you think it is so simple, but the actual const can play a lot of tricks:

1. const int *M, int* const m and int const *M

In fact, in addition to the first one, we rarely use the following two kinds of writing, but these two are legitimate C + + syntax, but the first one is very different from the wording. The const int *M represents the int value pointed to by the const modifier m pointer, meaning that the int value pointed to by the pointer is immutable.

int 9 ; Const int *m = &i; // Compile Error Ten ; // compile successm++;

int* Const m indicates that the m pointer value is const, but the value m points to is not, or is it a demonstration with code?

int 9 ; int const m = &i; // Compile errorm++; // Compile Success ten;

The int const *m seems to be less encountered, in fact, and the const int *m is a meaning that has a very easy-to-remember rule, that the const precedes the pointer symbol *, the value is const, otherwise the pointer is const.

C + + has a cast attribute, which represents the conversion of the type, and of course, the const can also be cast away, for example, we define the const modifier int value, if we want to modify it, there is no way, C + + provides const_cast< > To remove the const attribute of the variable.

int Main () {      constint9;      Const_cast<intten;                 }

However, this operation is not recommended because the const variable definition is not intended to change it. And this kind of operation is not safe, easy to cause many problems.

So why should we use the const?

1. At the beginning of the design of the const variable, you can avoid the wrong writing in the process of program development, for example, we define a person object ID, if you do not want to change the ID, it is defined as const, so that if there are other programmers trying to change the variable, it will be an error , so it is good to play a role in protecting variables.

2. When you see a const-modified variable, the reader knows that the variable is immutable, so it plays the role of self documenting (meaning that code is a comment).

3. The definition of a const variable is advantageous for compiler to optimize the program, to make the code more compact, or to perform more efficiently. It is easy to understand that Const is known in the compile-time compiler, so it can be boldly optimized (since it will not change once initialized).

4. Const in C + + is read-only memory, can be placed in unreadable areas such as ROM, in fact, the role and the last one said almost, in favor of the program's compilation run.

The next article will describe the function of the Const modifier, combining OOP in C + + to introduce the great role of const in member function decoration.

Advance C + +--Const

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.