C ++ basics: pointer and const qualifier

Source: Internet
Author: User

There are two types of interaction between the pointer and the const qualifier: the pointer to the const object and the const pointer. It is not difficult to understand the meaning of the two types (which will be introduced soon below). However, in a specific application environment, I believe many beginners will be confused. Especially for beginners looking for a job, if you cannot find out some detailed differences and connections between the const and pointer, it may cause trouble for your written examination or interview.

All the basic knowledge in this article comes from the fourth edition of the Chinese version of C ++ primer, p110 pages. Read it now :)

When the const is used to limit a basic object, the meaning is clearer. For example:

Const double c_pi = 3.14; // double const c_pi = 3.14;

The above two statements of code mean the same. In fact, when the const limits the type, there is no problem before and after the type (this will be further proven in a later experiment ).

However, when the pointer is added, the situation becomes a little more complicated.

1. pointer to the const object

Const double * cptr;

The above cptr is a pointer to a double-type const object. But cptr itself is not const, you can use to change the value of cptr pointer itself (to do pointer operations or direct it to another object ), however, cptr cannot be used to change the value of the object to which it points. Let's take a look at the following code:

Const double c_pi = 3.14; double Pi = 3.14; double * PTR; const double * cptr; // save space, write as a line

The following practices:

A. PTR = & c_pi; // error!

B. PTR = π

C. cptr = & c_pi;

D. cptr = π

The above code, a is incorrect (compilation error), and others are feasible. Note d, that is, allow non-const objects to be assigned a pointer to the const object. In this way, the PI value cannot be modified through cptr, but the PI value can be modified through other channels, that is, cptr only points to objects that cannot be modified after being referenced by cptr, but may still be modified in other ways (if the basic object itself is not const ).

In practical applications, the pointer to the const object is often used as a function parameter to ensure that the actual object passed to the function is not modified because of the parameter. Another purpose of pointing to the const Object Pointer is to point to the const object (you must use the "pointer to the const object" to point to the const object ).

2. Const pointer

Int num = 9;

Int * const c_ptr = & num;

The code above defines a const pointer, which can be practiced from the right to the left as "c_ptr is the const pointer, which points to the int type object ". The const pointer is the same as other const objects, that is, the value of the pointer itself cannot be modified, pointer operations cannot be performed, and it cannot be directed to another object, you cannot even assign the same value (for example, c_ptr = c_ptr; // Error !). However, you can use the pointer to unreference the base object to modify its value.

You may have understood it all. Well, let's take a look at the following code (don't be confused, because the goal of the code below is to confuse you,, my ultimate goal is to make you no longer confused, ohyeah! ):

Const int c_num = 5;

Int num = 6;

A. Int const * ptr_0;

B. Int const * PTR = & c_num;

C. Int const * ptr_1 = & num;

D. int * const ptr_2; // ----------------------- error!

E. int * const ptr_3 = & c_num; // ---------- error!

F. int * const ptr_4 = & num;

G. PTR = PTR;

H. * PTR = 7; // -------------------------------------- error!

I. * ptr_1 = 7; // ------------------------------------ error!

J. ptr_4 = ptr_4; // ------------------------------ error!

K. * ptr_4 = 7;

The above code is mainly used to check whether (INT const *) and (int * const) are two things. int * const, needless to say, is the const pointer; int const * is actually the same as const int. A simple memory method is to read definitions from right to left. The cause of the above Code error is as follows:

D The cause of the error is that the const pointer must be initialized;

E is wrong because it tries to point to the const object using the const pointer. The const object must point to the const object using the "pointer to the const object" instead of the const pointer. This deserves special attention!

H and I are used to demonstrate that using "pointer to const object" cannot change the basic object value, regardless of whether the object it points to is const;

J's error lies in trying to change the value of the const pointer.

Through the above description, I think we should all be able to differentiate the following statements:

Int * const PTR; // ------- error!

Int const * PTR;

Const int * PTR;

3. Const pointer to the const object

If you combine the two, it is the const pointer to the const object, which means to combine the two: you can neither modify the pointer value nor modify the object to which it points.

Const int c_num = 4;

Int num = 5;

Const int * const PTR = & c_num;

Cosnt int * const ptr_1 = & num;

The above code is correct. I believe there is no need to explain it more.

4. Add typedef.

If typedef is added, the problem becomes confused.

Typedef string * pstring;

Cosnt pstring CSTR;

Is CSTR (const string *) or (string * const )? The answer is the latter. If typedef is used as a text extension, it is very likely that the true meaning of CSTR is wrong. Let's look at the following code:

String STR;

Typedef string * pstring;

A. Const pstring cstr_1 = & STR;

B. pstring const cstr_2 = & STR;

C. String * const cstr_3 = & STR;

The const pointer is declared in A, B, and C above. Note that the relationship between const and type is uncertain again in A and B, c ++ primer suggests placing the const before the type.

5. Summary

I have nothing to say. I hope we will not be confused any more! PS: C ++ primer is very good and powerful!

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.