Top-level const and underlying const understanding in C + + primer

Source: Internet
Author: User

C + + primer in the top-level const and the underlying const previously did not understand the top-level const and the underlying const, this time to see the sense of understanding.

First, theconst is a qualifier, and the value of the variable it modifies cannot be changed .

For The general variable, there is no top-level const and the underlying const difference , and only to the pointer such a compound type of basic variables, there is such a difference.

how to distinguish the top-level const and the underlying const pointers If you add a const modifier in two cases: 1 pointers to constants: Represents a pointer that cannot change its point to the content. When declaring a const can be placed before or after the type name, in the case of the int type, when declared: const int and int const are equivalent. To declare a pointer to a constant, which is the underlying const, give an example:
    1. int num_a = 1;
    2. int Const *P_A = &num_a; //Bottom const
    3. *p_a = 2; Error, pointer to "constant" cannot change the object being referred to

Note: A pointer to "constant" does not mean that the content it points to must be constant, but that the representation cannot be changed by the dereference (operator *).  In the above example, the pointer p_a to the content is not a constant, you can pass the assignment statement: num_a=2; To change what it points to.

2 pointer constant : The pointer itself is a constant, which must be initialized when declared, and the address value it stores can no longer be changed. When declaring a const must be placed behind the pointer symbol *, that is: *const. Declaring a constant pointer is the top-level const, here's an example:

    1. int num_b = 2;
    2. int *Const P_B = &num_b; //top-level const
    3. P_b = &num_a; Error, constant pointer cannot change stored address value
In fact, the top-level const and the underlying const are simple. 1. Adding a const qualifier to a pointer itself is the top-level const. ( the pointer is limited to the top ) 2. Adding the const qualifier to the object that the pointer refers to is the underlying const. ( object is limited to the bottom)
Two distinguishing the role of the top-level const and the underlying constThere are two effects after differentiation. 1 there is a limit to the execution of object copies, and the underlying const of a constant cannot be assigned to a very low-level const. In other words, you can avoid such assignment errors if you can correctly differentiate between the top and bottom Const. Here's an example:
    1. int num_c = 3;
    2. const int *p_c = &num_c; //p_c is a pointer to the underlying const. Remember that you cannot modify the value of a variable by P_c
    3. int *p_d = P_c; Error, cannot assign the underlying const pointer to a non-underlying const pointer
    4. const int *p_d = P_c; //correct, you can copy the underlying const pointer to the underlying const pointer
2Use a named When you force a type conversion function to const_cast, you need to be able to distinguish between the underlying const and the top-level const, because const_cast can only change the underlying const of the operand。 Here's an example:
  1. int num_e = 4;
  2. const int *p_e = &num_e;
  3. *p_e = 5; Error, cannot change what the underlying const pointer is pointing to
  4. int *p_f = const_cast<int *> (p_e); //Correct, const_cast can change the underlying const of the operand.  But be sure to know that num_e is not a const type when used.
  5. *p_f = 5; //correct, non-top-level const pointer can change the point to the content
  6. cout << num_e; //Output 5

Top-level const and underlying const understanding in C + + primer

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.