Pointer to C ++ learning and const qualifier

Source: Internet
Author: User

Today I read section 4.2.5 "pointer and const qualifier" in C ++ primer ". I think it's like a tongue twister. It's easy to get it done, and it's messy. Do

Take notes for your reference.

1. pointer to the const object
(1) We can use a pointer to modify the value of the object it refers.
(2) If a pointer points to a const object, the pointer cannot be used to change the const value.
(3) to ensure this feature, C ++ requires that the pointer to the const object must also have the const feature.
Example:
Const double * cptr;
(1) cptr is a pointer to a double-type const object.
(2) const limits the object to which the cptr Pointer Points, rather than the cptr itself. That is, cptr itself is not of the const type. You do not need to initialize it during definition.

Start-Up: Allows cptr to be assigned a value to point to another const object.
(3) You cannot use cptr to modify the value of the object.
Note 1: assigning the address of a const object to a normal non-const object pointer will also cause compilation errors.
example:
const double Pi = 3.14;
double * PTR = & PI; // error: PTR is a plain pointer
const double * cptr = & PI; // OK: cptr is a pointer to const
NOTE 2: you cannot use the void * pointer to save the address of the const object. Instead, you must use a const void * pointer to save the address of the const object.
example:
const int universe = 42;
const void * CPV = & universe; // OK: CPV is const
void * Pv = & universe; // error: Universe is const
Note 3: allow non-const object addresses to pointer to const object
example:
double dval = 3.14; // dval is a double; its value can be changed
cptr = & dval; // OK: But can not change dval through cptr
although dval is not a const object, however, any attempt to modify its value through the pointer cptr will cause a compilation error. Once defined, cptr cannot be modified

The value of the object.
2. Const pointer
Const pointer --- the value of itself cannot be modified.
Int errnumb = 0;
Int * const curerr = & errnumb; // curerr is a constant pointer
Note: curerr is the const pointer to an int object. Like other const values, the value of the const pointer cannot be modified, which means that curerr cannot

To other objects.
Like any const volume, the const pointer must also be initialized during definition.
The pointer itself is the fact of const -- it does not explain -- whether the pointer can be used to modify the value of the object it points. Whether the value of the object indicated by the pointer can be modified depends entirely on

The object type.
3. Const pointer to the const object
Example:
Const double Pi = 3.14;
Const double * const pi_ptr = & PI;
In this example, you cannot modify the value of the object pointed to by pi_ptr or the pointer (that is, the address value stored in pi_ptr ).
Pi_ptr is a const pointer, pointing to a double-type const object.
4. pointer and typedef
Typedef string * pstring;
Cosnt pstring CSTR;
Q: What type is a CSTR variable?
A: Define CSTR as a const pointer to a string object.
This definition is equivalent to string * const CSTR;

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.