C ++ learning note one - 3.5 Const defined modifier

zhaozj2021-02-16  67

<1> .const type limits the modifier converts an object into a constant. For example, const Int buffs = 512.

<2>. Because constants cannot be modified after definition, it must be initialized. Constantized constant definitions will result in compilation errors. Const Double Pi; // Error: Unintegrated constant

<3>. Once a constant is defined, we cannot change the value associated with the Const object.

<4>. "Attempts to point a non-Const object point to a constant object" will cause compilation errors. We must declare a pointer to constant to do this. For example, const Double * cptr. "CPTR" is a pointer to the Const object. "The subtleness is that the CPTR itself is not constant. We can re-assapore CPTR to point to different objects, but cannot modify the CPTR pointing to the object .const object The address can only assign a pointer to the Const object, but the pointer to the Const object can be assigned to an address of a non-Const object.

<5> .int * const curer, "CURERR is a const pointer to non-Const object", which cannot give CURERR other address values, but can modify the value pointing to the CURERR, and it can only point to non-Const objects.

<6> .const Double * const pi, the value of the object pointing to the object and its address itself cannot be changed, and the PI is a const pointer to the Const object.

转载请注明原文地址:https://www.9cbs.com/read-24485.html

New Post(0)