The use of const in C + +

Source: Internet
Author: User

A const object, a pointer to a const object, a const pointer (known by a polygon question)

1.Const Object

(1) on the const, many enterprises of the written test, interview will appear, very simple, asked you "The meaning of const?" ”。

I do not know the answer, my first reaction is constant, then I think of C # about the const explanation (static and read-only), so I think of only read, I only think so much.

Let's take a look at the answer:

A: As soon as I hear the interviewee say "Const means constant", I know I am dealing with an amateur. Last year Dan Saks has completely summed up all the usages of const in his article, so ESP (translator: Embedded Systems programming) Every reader should be very familiar with what const can and cannot do. If you've never read that article, you can just say that const means "read-only". Although this answer is not a complete answer, I accept it as a correct answer. (If you want to know more about the answer, read Saks's article carefully.) )

:) own barely clearance, the following netizens provide a richer answer:

1). The function of the keyword const is to convey very useful information to the person who is reading your code, and in fact, declaring a parameter as a constant is to tell the user the purpose of the application of this parameter. If you have spent a lot of time cleaning up the rubbish left by others, you will soon learn to thank for the extra information. (Of course, it's very rare for a const programmer to leave rubbish for others to clean up.) ) 2). By giving some additional information to the optimizer, using the keyword const may produce more compact code. 3). The use of the keyword const can make it natural for the compiler to protect those parameters that you do not want to change, and to prevent them from being unintentionally modified by the code. In short, this can reduce the occurrence of bugs

This description of the const in the code to improve the "readability", "robustness" is still very effective.

2. pointers to const objects

(1)

Pointer to a const object const int i = 1024;//Note Initialize const int *IP = &i;

(2) Error compiling the address of the const object to the normal pointer

Const DOUBLE D = 3.14;double *DP = &d;//error: Because DP will default to the object it points to is modifiable, if correct, it means that the value of D can be modified by *DP, which is obviously not possible.

(3) Instead, the address of a non-const object is allowed to "pointer to a const object"

Double d = 3.14;const double *DP = &d;//ok: Similarly, DP default d is a const object, so it is not possible to modify the value of D by *DP

3.Const pointer (note: must be initialized when defined)

(1)

int i = 0;int *const IP = &i;

(2) * const pointer to const object

Const DOUBLE d = 3.14;const double *const DP = &d;

4.Tips

(1) When reading such expressions, it is best to "right-to-left" order, which is easy to understand

(2) In an expression we can interpret * as "point to", as

Const DOUBLE d = 3.14;const double *const DP = &D;//DP is const-point-to-double value which is const

The use of const in C + +

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.