Combining examples to illustrate how const and pointers in C + + are understood when combined

Source: Internet
Author: User

The difference between const int*, int* const, and const int* Const is briefly described in the previous essay, "The use of const in C + + (i)", remembering that three sentences can be used in practical use, but the nouns of pointer constants and constant pointers are found in reading. It is important to find out what these concepts are for reading articles.

Key: const and pointers combine time codes from right to left

1. Constant pointer (const pointer)

Concept: A constant is an adjective, meaning a constant pointer is a pointer to a const-decorated pointer.

As you read the code from right to left (concept nouns from left to right), try to write.

STEP1:I; Variable name

Step2:const i; The first noun is a const, written on the left side of I

step3:int* const I; The second noun is pointer (*), with the specific type int* as an example, written on the left side of the const I
2, pointer to the constant (pointer to const), generally referred to as the pointer constant (this translation is very bad, easy to confuse)

Concept: Pointing to a constant is an adjective, a pointer to a constant is also a pointer, but itself is not a const, the object pointed to is a const

Go ahead and try to write.

STEP1:I; Variable name

STEP2: * i; The first noun is pointer,* written on the left side of I

Step3:const int* i; The second noun is const, with the const int as an example, written on the left side of the *.

int const* i; This is also true, it can be understood to write a const on the left of the * I, to indicate a constant, and then write the specific type of the constant on the left side of const* I

3. Constant pointer to constant

In the same way, write the const on the left side of the variable name, and then write the * on the left of the const, then the left is the const int or int const.

int i = 0, j = 1;//constant pointer, p1 is const, p1 points to int (instead of const int) int* const P1 = &i;  Must be initialized because the object pointed to cannot be changed//P1 = &j; This sentence wants to assign the address of J to P1, and P1 is const*p1 = 2; point to int, no const modifier, can change//pointer to constant, p2 not const, P2 point to const intconst int* p2; Can not initialize (hanging pointer), anyway you can choose to point to the object p2 = &j; P2 = &i;  Arbitrarily modify the Pointing object//*p2 = 1;  Pointer to const int, cannot change//pointer to constant, P3 is const, P3 point is constconst int* const P3 = &i;//P3 = &j; Error, because P3 is const//*p3 = 3; Error, because P3 points to a const

For readability of code, it is common to use TypeDef to simplify pointers

The most notable usage of the previous one was to simplify the function pointers (pointers to functions) .

The code reading method for function pointers is from inside to outside

Int (*func) (int, int);

How to read this code:

STEP1: The innermost is the variable name func, which is a function pointer

STEP2: Read left, func returns int, read right, func argument is 2 int

A little more complicated ...

Float (* (*func) (int,int)) (int);

STEP1: The innermost is the variable name func, which is a function pointer, accepts 1 int parameters, returns a function pointer

STEP2: The function pointer returned is FUNC1, which is typedef float (*FUNC1) (int);

STEP3: This is good to read, FUNC1 accepts 1 int parameters, returns float.

All right, back to the chase. One thing to note with TypeDef is that it cannot be equivalent to C # # # #, although it is really used instead of # define in C + +.

Because typedef is not a direct replacement of code, this code is treated as a type.

Just on the code description.

typedef int* Pint;int i = 0, j = 1;//here PInt as a temporary unknown basic data type, stating P1 is const//to see the specific type of pInt, is a pointer to int//That is, P1 is a pointer, p1 is a const, Pointer to int (not const int) const PINT P1 = &i;  Equivalent to pint const or int* const//p1 = &j; Error *P1 = 2;int *p2 = &i, *p3 = &j;int **pp1 = &p2, **pp2 = &p3;//pp2 is a pointer to a const PINTCONST pint* pp3 ;PP 3 = PP1;PP3 = PP2; You can change the object that you are pointing to//*pp3 = P2; Error, PP3 points to the const type (const PINT) and cannot be modified by * (*PP3) = 4;  Correct, [Pp3 point to const PINT] points to pInt (this pInt points to an int that can be modified)

In addition, the concept of the top level (top-level) const and the underlying (low-level) const is also seen on the C + + primer

underlying const: Object pointed to cannot be modified (e.g. const int*)

top-level const: The pointer or variable itself cannot be modified (such as const int, int* const)

Const INT* Const combines the underlying and top-level properties

Key 1: Only pointers with the underlying const nature can be converted to the same underlying const qualification!

For example, the const int* const combines the underlying and top-level const properties.

int i = 1;const int* p1 = &i;const int* const P2 = &i;p1 = p2;//P2 = p1; Error because the P2 itself cannot be modified

In fact, it is not known that the top layer can be understood, just like a const t can be converted to T. (t is data type)

t& cannot be converted to const t&,t& nor can it bind a const T (common error, mentioned in the "key points of use in C + + (ii)")

t* cannot convert you to a const t*. However, pointers are a bit more specific, such as being able to cast like the following code

Const int* PINT1 = &i;int* PInt2 = &i;//can be cast pInt2 = (int*) Pint1;pint2 = const_cast<int*> (pInt1);

The key 2:auto type can only speculate on the underlying const nature!

Auto A = &ci;  A is a const int*, and the underlying const retains auto B = ci;   b is int, top-level const is ignored

For this issue, C + + 11 adds the Decltype type, which preserves the top-level const.

For example, the second line of the above code is decltype (b) = Ci, and after B is the const int type

Combining examples to illustrate how const and pointers in C + + are understood when combined

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.