Top-level const and underlying const in C + +

Source: Internet
Author: User

1. Top-level const and underlying const concept

The pointer itself is an object, because the pointer actually corresponds to a piece of storage space of the memory unit, however, the pointer is also a data object, so the pointer is a constant and pointer to a constant is two completely different concepts,top-Level constIndicates that theThe pointer itself is a constant,underlying constIndicates that theThe object that the pointer refers to is a constant, more generally,a top-level const can indicate that a random object is a constant, which is true for arithmetic types, classes, pointers, and so on, regardless of data type,The underlying const is related to the basic type part of a composite type such as pointers and references, the more special is that the pointer can be either the top-level const or the underlying const, which differs significantly from the other types.

2. The difference between the top-level const and the underlying const when running copy operations

For the top-level const and the underlying const, there is a distinct difference in running the object copy:

(1) Top-level const is unaffected

[CPP]View Plaincopyprint?
  1. int  i = 0;
  2. Const    int ci = 42; //cannot change the value of CI, which is a top-level const
  3. i = CI; //Correct: CI is a top-level const that has no effect on this operation   
  4. Const   int *P2 = &ci; //Agree to change the value of P2, which is an underlying const
  5. Const   int *const p3 = p2; //The const on the right is the top-level const, and the left is the underlying const
  6. P2 = p3; //correct: The type of object that P2 and P3 point to Also, the part of the P3 top-level const does not affect   


(2) The limitations of the underlying const cannot be ignored,requires that the copied and copied objects have the same underlying const qualification or can be converted to the same data type, generally can be converted to a constant, and vice versa

[CPP]View Plaincopyprint?
  1. int  *p = p3; //Error: P3 contains the underlying const definition, and P does not
  2. P2 = p3; //correct: P2 and P3 are the underlying const   
  3. P2 = &i; //correct: int* can be converted to const int*   
  4. int  &r = ci; //Error: Normal int& cannot be bound to an int constant
  5. Const   int &r2 = i; //correct: const int& can be bound to a normal int

int *p = p3;
P3 is both a top-level const and a bottom-level const, when the object copy is run, the top const part does not have any influence, regardless of the effect, but P3 is also a bottom const, it requires the copied object has the same underlying const qualification, and p does not, so is wrong;

p2 = p3;
P3 require the object to be copied into the same level of const qualification, P2 is also a lower-level const, it is correct;

p2 = &i;
To I take address will getint*, p2 isconst int*, the former is a lot, the latter is a constant, assignment statement equals right side of the type to the left side of the conversion, the amount can be converted to the constant, it is correct;

int &r = ci;
From the left side we know that what is required is a reference that is bound to a CI, and the type of reference that is bound to the CI isconst int&, the type of the left side of the equals sign isint&, the type on the right side of the assignment statement is converted to the left, but the constant cannot be converted to a very good amount, so it is wrong;

const int &r2 = i;
From the left side we know that what is needed is a reference bound to I, and the type of reference bound to I isint&, the type of the left side of the equals sign isconst int&, the type on the right side of the assignment statement is converted to the left side, which is usually a lot to convert to, so it is correct.

It is important not only to be aware of the nature of const at the same time that the type conversion restriction

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.