Pointer to const object, const pointer, and const pointer to const object in C ++

Source: Internet
Author: User

This is a tough thing to remember, because it has never been used. To sum up, I will not check it on C ++ primer later.


Const pointers are classified into three types:
1. pointer to the const object (1) definition form: const double * PT; // const is at the beginning of the definition. (2) Key: Pt can change the point but cannot change the value of the object to which it points. That is, its own value can be changed, but PT cannot be used to change the value of the object to which it points. Obviously, PT is a pointer to a const object. How can a const object be changed? However, Pt can also point to a non-const object, but treat it as a const object.
2. Const pointer (1) definition form: Double * const pt; // const in the middle of definition (2) Key: Pt cannot be changed, the value of the object it points to can be changed depending on the object itself. That is, the PT value cannot be changed. When Pt points to a const object, PT cannot be used to change the value of this object. When Pt points to a non-const object, you can use pt to change the value of this object.
3. Const pointer to the const object (1) definition form: const double * const pt; // There are two const, one at the beginning of the definition and the other in the middle of the definition. (2) Key: Pt neither changes the point nor the value of the object it points. That is, the value of PT itself cannot be changed, and the value of the object to which it points cannot be modified.
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.