C + + const

Source: Internet
Author: User

The type of the pointer must be consistent with the object type it refers to allow pointers to constants to be obtained by a very mass object, to change values; A pointer to a constant does not specify that the object it refers to must be a constant a pointer to a constant simply requires that the object value cannot be changed by the pointer. Instead of specifying that the value of that object cannot be changed by other means to change a pointer or reference to a constant, pointing to a constant, consciously not changing the value of the object; The const pointer pointer is an object and the reference is not, the pointer itself can be fixed as a constant constant pointer must initialize the const pointer constant Once the pointer is initialized, its value-the value stored in the constant pointer-the address can no longer be changed by placing the * before the const keyword to indicate that the pointer is constant and that the pointer itself is the value rather than the value pointed to.

  

intErrnumb =0;int*ConstCurerr = &errNumb;//Curerr will always point to ErrnumbConst DoublePI =3.14;Const Double*ConstPip = Π PIP is a constant pointer to a constant object. The most effective way to clarify the meaning of a statement--reading from right to left the pointer itself is a constant and does not mean that the value it points to cannot be modified by the pointer. What cannot be changed is that this pointer can no longer point to another object PIP is a constant pointer to a constant, regardless of the value of the object or Pip's own stored address value can not change whether the pointer itself is a constant and the pointer is not a constant refers to is two independent problem pointer itself is an object, It can also point to whether the other object pointer itself is a constant and the pointer refers to a constant that is two independent issues top-level const-levelConst) indicates that the pointer itself is a constant underlying const ( low-levelConst) represents the underlying const with pointers and referencesinti =0;int*ConstP1 = &i;//pointer itself constant, pointer no longer points to another objectConst intCI = the; An object is not a constant expression determined by its data type and initial value to declare the variable declared as constexpr by the compiler to validate the variable's value as constexpr variable must be constant although you cannot use a normal function

C + + const

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.