Usage of the Const keyword in C + +

Source: Internet
Author: User

1.const is used to modify ordinary variables, indicating constants, not recommended modifications, to a certain extent do not allow the modification (in fact, can be modified)

Pointer constant: The pointer (the value of the variable pointed to) itself is a constant, stating that it cannot change its own point to int* const p=&a;
Constant pointer: A pointer to a constant that can be modified to point to, but cannot modify, the value of a variable that points to a const int* p;
Interview must kill Skill:
Const appears on the left of * is a constant pointer
const int num = 19;
const INT * p = #//Pointer to constant

Common interview questions often take the following examples to fool people:

intMain () {intA =Ten; intb = -; Const int* p;//Const *P, which can be used without initialization, to indicate that *p is constant    int*ConstP1 = &b;//const P, must initialize, otherwise error, indicates p is constant address    int* PP1Const= &a;//error, no such wording, different from the usual function: int fun () const;    Const int*Constp2=&a;//The const int* and int const* are the same, and must be initialized at the time of definition, because the const directly modifies the pointer P2    int Const*Constp3=&a;//P2 and P3 are the same effect, just consider the const on the left or right side of the *, not affected by the int}

Usage of the Const keyword 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.