C ++ const object pointer and const pointer (8)

Source: Internet
Author: User

(1) Example: 1. Int I = 1;
Const int * cptr = & I;
* Cptr = 2; // error. cptr points to constant 1.
Cout <* cptr <Endl ;//
2. Int I = 1;
Const int * cptr = & I;
I = 2; // normal
Cout <* cptr <Endl; // output 2
3. Int I = 1, TT = 8;
Const int * cptr = & I;
Cptr = & tt; // normal
Cout <* cptr <Endl; // output 8
To sum up, the only result caused by const pointer is that * cptr =... cannot be used, and others can be used (cptr itself can be changed, but its point cannot be changed)
But above Program It doesn't make sense. The const Pointer Points to the const int...
Const int A = 1;
Const int * cptr = & A or const void * cptr = & A; // constant, the pointer must also be a constant
Note: const pointers are commonly used as function parameters. Define the form parameter as a pointer to const to ensure that the actual object passed to the function is not modified because of the form parameter in the function.

(2) The value of const pointer itself cannot be changed, but whether it points to can be changed based on whether the object to be pointed to is const.
Int I = 10, TT = 88; // an error occurs if it is const int I = 10.
Int * const cptr = & I;
* Cptr = 9; // Yes
Cptr = & tt; // Error
Cout <* cptr <Endl;
(3) The const pointer to a const Object Pointer itself and the object to which it points cannot be changed.
Const int I = 10;
Const int * const cptr = & I;

conclusion: If const is added, it is displayed from the right to the left, that is, the right to the left.
for example, const int * cptr is an int pointer, which is a constant
int * const cptr (pointer) is const type. This const points to int
const int * const cptr = & I; cptr is const type, while 2nd const points to int type, and this int is a constant

Original variable: int type --> pointer variable int * const P, const int *, const int * const Pi = &;
Const int --> pointer variable const int * P, const int * const Pi = &;

Additional:
1 const string STR;
String const STR; // The same as the two
2 string S;
Typedef string * pstring;
Const pstring cstr1 = & S; // 1 Type
Pstring const cstr2 = & S; // two types of all three decreations are the same type
String * const cstr3 = & S; // they're all const pointers to string

)

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.