C + + pointers and const

Source: Internet
Author: User

  In C + +, theconst modifier is typically used to modify constants. Constants must be initialized at the time of definition and cannot be modified once the value is defined, thus guaranteeing that the value of the constant will not change during the program's operation.

1. Pointers to const objects

A pointer to the const object itself can be transformed, but it is not possible to modify the value of the pointing variable through the pointer. A pointer to a const object does not need to be initialized when defined, and this type of pointer can also point to a normal variable, except that the value of the corresponding variable cannot be modified by the pointer, even if the corresponding variable is not a constant. pointers to constants must be decorated with the const modifier before the type identifier, otherwise the compilation does not pass.

The definition and usage of pointers to const objects are as follows:

Const intA=1;intb=1; //pointers to constants do not need to be initialized when definedConst int*cptr;//Put the const before or after the type, and the meaning is exactly the sameint Const*cptr;int*ptr;cptr= &A;*cptr =2 ;//illegal, you cannot modify the value of a pointer to a variable by using thecptr=&b;//illegal, you cannot modify the value of a pointer to a variable by using it, even if the variable pointed to is not a constantptr=&b;*ptr=3;//Legal! ptr=&A;//illegal, a pointer to a constant enforces the use of the const modifier before the type identifier, otherwise the compilation does not pass

2.const pointer

The const pointer itself is a constant whose value must be initialized at the time of definition, and the address pointed to by the const pointer cannot be modified after initialization. A const pointer cannot point to a constant (unless it is a const pointer to a const object), but you can modify the value of the variable it points to by using a const pointer.

The definition and usage of the const pointer are as follows:

intA=1;Const intb=2;int*Constcptr;//illegal, the const pointer is initialized at the time of definitionint*count cptr=&b;//illegal, the const pointer cannot point to a const object unless the pointer is a const pointer to a const objectint*ConstCptr =&A;*a=2;//You can modify the value of the variable it points to by using the const pointer

3. Const pointer to const object

A const pointer to a const object has properties for pointers in both 1 and 2. You cannot change the value of a pointer to an object first, even if the object it points to is a constant (or it can point to a generic variable, but you cannot modify the value of the variable pointed to by the pointer), and once the value of the pointer is initialized, it cannot be modified, and the value of the pointer must be initialized at the time of definition.

The definition and use of a const pointer to a const object is as follows:

Const intA =1;intb=2;Const int*Constcptr;//illegal, must be initialized at the time of definitionConst int* cosnt cptr =&A;int Const*ConstCptr =&A;//the definition of the above two sequences is exactly the sameConst int*ConstCptr_1 =&b;//can point to generic variables*cptr_1 =1;//illegal, you cannot change the value of the object it points to by this pointer, even if the object pointed to is not a constant*cptr=2;//illegal, cannot change the value of the object it points to by this pointer

Appendix: Pointers and typedef

The following data types are defined using typedef:

string * pstring; Const  const CSTR1;

So what are the real data types of CStr and cstr1? At this point it is necessary to realize that the const modifier is the type of pstring, which is a pointer, so const is a direct modifier of the pointer! Thus the data types of CStr and CSTR1 are-

string const PTR;

Reference:C + + Primer 3rd

 

C + + pointers and const

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.