Focus on C + + details--Universal const qualifier and Pointer __c++

Source: Internet
Author: User

Do not remember who said, can use the const when possible to use the Const. Indeed, Const makes a great contribution to increasing the robustness of the program, but the const modifier pointer is noteworthy.

A pointer to a const object;

If the pointer is to a const object, it is clear that the const value it points to is not changed by the pointer. To ensure this feature, the C + + language enforces that pointers to the Const object must also have a const attribute.

const DOUBLE = 1.0;

const double *CPT;

cpt=&d;

Why the const-type pointer variable can be assigned a value again. In fact, the CPT here is a pointer to a double type const object, which is defined by the type of object that the CPT pointer points to, not the CPT itself. In other words, CPT itself is not a const. Therefore, CPT does not need to be initialized at the time of definition, allowing the CPT to be assigned a value, pointing to another const object.

Note: You can assign an ordinary object's address to a pointer to a const object, but you cannot assign a const object address to an ordinary type pointer, otherwise a compilation error occurs. You cannot modify the underlying object with a pointer to a const object. Whether the const pointer points to an object that is not a const type, the system treats all objects it refers to as a const, limited to the const object of the pointer (through which the value cannot be modified);

Two. Const pointer;

In addition to pointers to the const object, the C + + language provides a const pointer--its value cannot be modified.

int inum = 1;

int * Const icpt=&inum;

The ICPT here is the const type--no longer pointing to other objects. Any attempt to assign values to the const pointer results in a compilation error. It is worth noting that the const type of the pointer itself does not indicate whether the pointer can be used to modify the value of the object it points to. Whether the value of the object that the pointer refers to can be modified depends entirely on the type of object.

int inum = 5;

int * Const icpt=&inum;

*icpt=4;//here is perfectly OK, because inum is a normal variable.

Three. Point to const object Const pointer;

Here is the combination of the two cases where the const type pointer points to a const type object.

const double pi=3.14;

Const DOUBLE * Const pt=π

Today summed up so much, welcome everyone to learn to exchange messages.


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.